Skip to main content
  1. About
  2. For Teams
added 7 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101

PHP: fastest approach 62ms (69 years old CPU!)

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 4790k i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)

PHP: fastest approach 62ms (6 years old CPU)

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)

PHP: fastest approach 62ms (9 years old CPU!)

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 4790k i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)
added 22 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101

PHP: fastest approach 62ms fastest approach 62ms (6 years old CPU)

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)

PHP: fastest approach 62ms

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)

PHP: fastest approach 62ms (6 years old CPU)

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)
deleted 26 characters in body; deleted 5 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101

First approachPHP: 205msfastest approach 62ms

Using PHP, a pre-filled referencean intermediate array of iterators, a robust old tech for loop, descending without sorting of values, so the most occurrence isbut direct search trough the first keyiterator array, sinceusing arsortarray_search() does maintain key associationand max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort$repeat = max($temp);
 
$result = array_key_firstarray_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result]$repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best runThis yield an impressive improvement, on mythe same machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667062928915023804 second(s)

2ndFirst approach: 62ms205ms

Still using an intermediateUsing a pre-filled reference array of iterators but instead of, a robust old tech for loop, descending sorting to retrieveof values, so the most occurrence is the first key, it search directly trough using array_search( andsince maxarsort() on the iterator arraydoes maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = maxarsort($temp); 

$result = array_searcharray_key_first($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat$temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement,Best run on the samemy machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.06292891502380420546293258667 second(s)

First approach: 205ms

Using PHP, a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);
 
$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)

2nd approach: 62ms

Still using an intermediate array of iterators but instead of sorting to retrieve the key, it search directly trough using array_search( and max( on the iterator array.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

PHP: fastest approach 62ms

Using an intermediate array of iterators without sorting but direct search trough the iterator array, using array_search() and max().

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);      // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

$repeat = max($temp);
$result = array_search($repeat, $temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $repeat . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

This yield an impressive improvement, on the same machine :

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.062928915023804 second(s)

First approach: 205ms

Using a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp); 

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Best run on my machine (gen5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.20546293258667 second(s)
added 12 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
added 1 character in body; deleted 12 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
deleted 2 characters in body; deleted 2 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
added 18 characters in body; deleted 9 characters in body; added 66 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
added 890 characters in body; added 10 characters in body; added 4 characters in body; added 5 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
added 25 characters in body; deleted 1 character in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
edited body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
added 30 characters in body; added 2 characters in body
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
Source Link
NVRM
  • 13.4k
  • 2
  • 102
  • 101
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.