3v4l.org

run code in 300+ PHP versions simultaneously
<?php function returnTime(callable $function, int $repeat = 50) { $tests = []; for ($i = 0; $i < $repeat; ++$i) { $startTime = microtime(true); $function(); $endTime = microtime(true); $tests[] = $endTime - $startTime; } // Representing the average return 1000 * array_sum($tests) / $repeat; } $array = array_fill(0, 200000, 1); echo "Duration of array_slice() + key(): ", returnTime(function() use ($array) { $lastKey = key(array_slice($array, -1, 1, true)); }); echo PHP_EOL; echo "Duration of end() + key(): " , returnTime(function() use ($array){ end($array); $lastKey = key($array); }); echo PHP_EOL; echo "Duration of array_key_last(): " , returnTime(function() use ($array){ $lastKey = array_key_last($array); }); echo PHP_EOL; echo "Duration of array_keys() + end(): " , returnTime(function() use ($array){ $keys = array_keys($array); $last = end($keys); }); echo PHP_EOL; echo "Duration of array_reverse() + key(): " , returnTime(function() use ($array){ $key = key(array_reverse($array)); }); echo PHP_EOL; echo "Duration of array_slice() + foreach(): " , returnTime(function() use ($array){ foreach (array_slice($array, -1, 1, true) as $key => $value); });

preferences:
32.38 ms | 413 KiB | 5 Q