Skip to content

Instantly share code, notes, and snippets.

@Pierstoval
Last active August 23, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pierstoval/12246b196b060037cfce3aae95d63920 to your computer and use it in GitHub Desktop.
Save Pierstoval/12246b196b060037cfce3aae95d63920 to your computer and use it in GitHub Desktop.
Benchmarking array_key_last php function from version 7.3. | Script based on http://www.php-benchmark-script.com
---------------------------------------------------
| PHP BENCHMARK SCRIPT |
---------------------------------------------------
Start : 2018-08-23 08:37:29
PHP version : 7.3.0beta2
Platform : Linux
Each test executed 10000 times
---------------------------------------------------
test_array_reverse 650.660 sec.
test_array_key_last 0.049 sec.
test_array_key_last_polyfill 216.517 sec.
---------------------------------------------------
Total time 867.228 sec.
---------------------------------------------------
| PHP BENCHMARK SCRIPT |
---------------------------------------------------
Start: 2018-08-23 09:02:07
PHP version: 7.3.0beta2
Platform: Linux
Each test is executed 100000 times
---------------------------------------------------
test_array_key_last 2.726 sec.
test_array_key_last_polyfill 791.382 sec.
test_array_reverse 1,585.311 sec.
---------------------------------------------------
Total time 2,379.420 sec.
---------------------------------------------------
| PHP BENCHMARK SCRIPT |
---------------------------------------------------
Start: 2018-08-23 09:03:10
PHP version: 7.3.0beta2
Platform: Linux
Each test is executed 100000 times
---------------------------------------------------
test_array_key_last 2.880 sec.
test_array_key_last_polyfill 827.293 sec.
test_array_reverse 1,650.330 sec.
---------------------------------------------------
Total time 2,480.503 sec.
<?php
const PROGRESS = true;
$arrayToTest = range(1, 250000);
function array_key_last_polyfill(array $array) { $key = null; foreach ($array as $key => $value); return $key; }
class tests
{
public static function test_array_key_last()
{
global $arrayToTest;
$key = array_key_last($arrayToTest);
}
public static function test_array_key_last_polyfill()
{
global $arrayToTest;
$key = array_key_last_polyfill($arrayToTest);
}
public static function test_array_reverse()
{
global $arrayToTest;
$rev = array_reverse($arrayToTest, true);
end($rev);
$key = key($rev);
}
}
class benchmarker
{
private const COUNT = 100000;
/**
* This is just the script that will run the benchmark.
*/
public static function run()
{
$total = 0;
$methodText = "Executing %s... %d/%d\r";
$line = str_pad('-', 51, '-');
echo "$line\n|"
.str_pad('PHP BENCHMARK SCRIPT', 49, ' ', STR_PAD_BOTH)."|\n".
"$line\n".
' Start: '.date('Y-m-d H:i:s')."\n".
' PHP version: '.PHP_VERSION."\n".
' Platform: '.PHP_OS."\n".
' Each test is executed '.self::COUNT." times\n".
"$line\n"
;
foreach (get_class_methods(tests::class) as $method) {
$time_start = microtime(true);
if (!PROGRESS) {
printf(' Executing %s...', $method);
}
for ($i = 1; $i <= self::COUNT; $i++) {
if (PROGRESS) {
printf($methodText, $method, $i, self::COUNT);
}
tests::$method();
}
$result = microtime(true) - $time_start;
$total += $result;
echo str_pad(" $method".' ', 40).str_pad(number_format($result, 3).' sec.', 10, ' ', STR_PAD_LEFT).
str_pad('', strlen($methodText) + strlen((string) self::COUNT) * 2 + strlen($method) + 4, ' ').
"\r\n"
;
}
echo
"$line\n".
str_pad(' Total time', 40).number_format($total, 3).' sec.'
;
}
}
benchmarker::run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment