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)