3v4l.org

run code in 300+ PHP versions simultaneously
<?php function returnTime(callable $function) { $count = 100; $repeat = 100; $tests = []; for ($i = 0; $i < $repeat; $i++) { $tests[$i] = []; for ($i = 0; $i < $count; $i++) { $startTime = microtime(true); $function(); $endTime = microtime(true); $tests[$i][] = $endTime - $startTime; } } foreach ($tests as &$test) $test = array_sum($test); return array_sum($tests) / $repeat; } function compareResults($result1, $name1, $result2, $name2) { $results = [ $result1, $result2, ]; $names = [ $name1, $name2, ]; if ($results[0] == $results[1]) { return 0; } if ($results[0] < $results[1]) { $fasterIndex = 0; } else { $fasterIndex = 1; } $slowerIndex = 1 - $fasterIndex; $fasterResult = $results[$fasterIndex]; $slowerResult = $results[$slowerIndex]; echo "{$names[$fasterIndex]} performs faster than {$names[$slowerIndex]}.", PHP_EOL; echo "It is ", round(($slowerResult / $fasterResult) * 100 - 100), "% faster.", PHP_EOL; } // Array with 500000 elements $array = array_fill(0, 500000, 1); compareResults( returnTime(function () use ($array) { end($array); $lastKey = key($array); reset($array); }), "end()", returnTime(function () use ($array) { $lastKey = key(array_slice($array, -1, 1, true)); }), "array_slice()" );

Abusive script

This script was stopped while abusing our resources

Output for 8.2.11
array_slice() performs faster than end(). It is 2056589% faster.
Output for 8.2.0
array_slice() performs faster than end(). It is 2205555% faster.
Output for 8.1.11 - 8.1.24
Fatal error: Out of memory (allocated 18878464) (tried to allocate 16777224 bytes) in /in/17XLq on line 57 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.4.4
array_slice() performs faster than end(). It is 933% faster.
Output for 7.2.29, 7.4.3
array_slice() performs faster than end(). It is 994% faster.
Output for 7.4.2
array_slice() performs faster than end(). It is 1155% faster.
Output for 7.4.1
array_slice() performs faster than end(). It is 925% faster.
Output for 7.4.0
array_slice() performs faster than end(). It is 1042% faster.
Output for 7.3.16
array_slice() performs faster than end(). It is 1054% faster.
Output for 7.3.11, 7.3.15
array_slice() performs faster than end(). It is 954% faster.
Output for 7.3.14
array_slice() performs faster than end(). It is 1062% faster.
Output for 7.3.13
array_slice() performs faster than end(). It is 878% faster.
Output for 7.3.12
array_slice() performs faster than end(). It is 946% faster.
Output for 7.3.10
array_slice() performs faster than end(). It is 858% faster.
Output for 7.3.9
array_slice() performs faster than end(). It is 983% faster.
Output for 7.3.8
array_slice() performs faster than end(). It is 963% faster.
Output for 7.3.7
array_slice() performs faster than end(). It is 1050% faster.
Output for 7.3.6
array_slice() performs faster than end(). It is 950% faster.
Output for 7.3.5
array_slice() performs faster than end(). It is 975% faster.
Output for 7.2.19, 7.3.4
array_slice() performs faster than end(). It is 1074% faster.
Output for 7.3.3
array_slice() performs faster than end(). It is 985% faster.
Output for 7.1.28, 7.1.30 - 7.1.33, 7.2.15, 7.3.2

Process exited with code 137.
Output for 7.3.1
array_slice() performs faster than end(). It is 553% faster.
Output for 7.3.0
array_slice() performs faster than end(). It is 446% faster.
Output for 7.2.28
array_slice() performs faster than end(). It is 961% faster.
Output for 7.2.27
array_slice() performs faster than end(). It is 1008% faster.
Output for 7.2.26
array_slice() performs faster than end(). It is 959% faster.
Output for 7.2.17, 7.2.25
array_slice() performs faster than end(). It is 1044% faster.
Output for 7.2.24
array_slice() performs faster than end(). It is 980% faster.
Output for 7.2.23
array_slice() performs faster than end(). It is 1081% faster.
Output for 7.2.22
array_slice() performs faster than end(). It is 1096% faster.
Output for 7.2.21
array_slice() performs faster than end(). It is 1058% faster.
Output for 7.2.20
array_slice() performs faster than end(). It is 1000% faster.
Output for 7.2.18
array_slice() performs faster than end(). It is 951% faster.
Output for 7.2.16
array_slice() performs faster than end(). It is 932% faster.
Output for 7.2.14
array_slice() performs faster than end(). It is 540% faster.
Output for 7.2.13
array_slice() performs faster than end(). It is 393% faster.
Output for 7.2.12
array_slice() performs faster than end(). It is 414% faster.
Output for 7.2.11
array_slice() performs faster than end(). It is 562% faster.
Output for 7.2.10
array_slice() performs faster than end(). It is 483% faster.
Output for 7.2.9
array_slice() performs faster than end(). It is 394% faster.
Output for 7.2.8
array_slice() performs faster than end(). It is 465% faster.
Output for 7.2.7
array_slice() performs faster than end(). It is 409% faster.
Output for 7.2.6
array_slice() performs faster than end(). It is 848% faster.
Output for 7.2.5
array_slice() performs faster than end(). It is 637% faster.
Output for 7.2.4
array_slice() performs faster than end(). It is 522% faster.
Output for 7.2.3
array_slice() performs faster than end(). It is 752% faster.
Output for 7.2.2
array_slice() performs faster than end(). It is 534% faster.
Output for 7.2.1
array_slice() performs faster than end(). It is 514% faster.
Output for 7.2.0
array_slice() performs faster than end(). It is 510% faster.
Output for 7.1.29
array_slice() performs faster than end(). It is 1212% faster.
Output for 7.1.27
array_slice() performs faster than end(). It is 1110% faster.
Output for 7.1.26
array_slice() performs faster than end(). It is 628% faster.
Output for 7.1.25
array_slice() performs faster than end(). It is 508% faster.

preferences:
55.04 ms | 480 KiB | 5 Q