3v4l.org

run code in 300+ PHP versions simultaneously
<?php $testList = [ [ 'array' => [], 'expect' => true, ], [ 'array' => ['a', 'b', 'c'], 'expect' => true, ], [ 'array' => ["0" => 'a', "1" => 'b', "2" => 'c'], 'expect' => true, ], [ 'array' => ["1" => 'a', "0" => 'b', "2" => 'c'], 'expect' => false, ], [ 'array' => ["a" => 'a', "b" => 'b', "c" => 'c'], 'expect' => false, ], ]; function displayBenchmark($function) { global $testList; echo '<h5>use function <code>' . $function . '</code></h5>'; $i = 0; $startTime = microtime(true); for ($i = 0; $i <= 1000; ++$i) { foreach ($testList as $testItem) { $result = $function($testItem['array']); assert($result === $testItem['expect'], 'Unexpect matched on index ' . $i); ++$i; } } $totalTime = microtime(true) - $startTime; echo 'total time: ' . var_export($totalTime, true) . '<br>'; } function array_is_list1(array $arr) { if ($arr === []) { return true; } return array_keys($arr) === range(0, count($arr) - 1); } function array_is_list2(array $array): bool { $i = -1; foreach ($array as $k => $v) { ++$i; if ($k !== $i) { return false; } } return true; } class Benchmark { private static $max, $memory; public static function memoryTick() { self::$memory = memory_get_usage() - self::$memory; self::$max = self::$memory>self::$max?self::$memory:self::$max; self::$memory = memory_get_usage(); } public static function benchmarkMemory(callable $function, $args=null) { declare(ticks=1); self::$memory = memory_get_usage(); self::$max = 0; register_tick_function('call_user_func_array', ['Benchmark', 'memoryTick'], []); $result = is_array($args)? call_user_func_array($function, $args): call_user_func($function); unregister_tick_function('call_user_func_array'); return [ 'memory' => self::$max ]; } } var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list'])); var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list1'])); var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list2']));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.110.0090.01218.42
8.3.10.0070.01421.88
8.3.00.0110.00718.84
8.2.140.0070.01820.47
8.2.130.0030.01720.53
8.2.120.0090.00618.82
8.2.110.0130.00919.28
8.2.100.0080.00819.05
8.2.90.0030.01319.00
8.2.80.0100.00618.95
8.2.70.0090.00918.88
8.2.60.0100.01318.74
8.2.50.0140.00718.64
8.2.40.0190.00018.89
8.2.30.0060.00918.68
8.2.20.0030.00618.69
8.2.10.0130.00718.78
8.2.00.0090.00018.53
8.1.270.0150.00422.19
8.1.260.0130.00622.12
8.1.250.0100.01022.02
8.1.240.0090.00018.75
8.1.230.0090.00918.62
8.1.220.0090.00618.82
8.1.210.0100.00618.55
8.1.200.0070.01418.81
8.1.190.0140.00718.80
8.1.180.0130.00918.51
8.1.170.0070.01018.52
8.1.160.0030.00618.60
8.1.150.0040.01218.49
8.1.140.0090.00618.51
8.1.130.0090.00018.72
8.1.120.0080.00418.53
8.1.110.0060.00318.54
8.1.100.0060.00318.57
8.1.90.0090.00018.66
8.1.80.0090.00018.58
8.1.70.0000.00818.47
8.1.60.0030.00618.73
8.1.50.0060.00318.63
8.1.40.0080.00018.75
8.1.30.0090.00018.87
8.1.20.0030.00618.69
8.1.10.0030.00618.61
8.1.00.0080.00018.57
7.4.330.0170.00217.30
7.2.160.0180.00922.96

preferences:
28.29 ms | 406 KiB | 6 Q