3v4l.org

run code in 300+ PHP versions simultaneously
<?php // array_values function method_1(Array &$arr) { return $arr === array_values($arr); } // method_2 was DQ; did not actually work // array_keys function method_3(Array &$arr) { return array_keys($arr) === range(0, count($arr) - 1); } // foreach function method_4(Array &$arr) { $idx = 0; foreach( $arr as $key => $val ){ if( $key !== $idx ) return FALSE; ++$idx; } return TRUE; } // guessing function method_5(Array &$arr) { global $METHOD_5_KEY; $i = 0; $l = count($arr)-1; end($arr); if ( key($arr) !== $l ) return FALSE; reset($arr); do { if ( $i !== key($arr) ) return FALSE; ++$i; next($arr); } while ($i < $l); return TRUE; } // naieve function method_6(Array &$arr) { $i = 0; $l = count($arr); do { if ( NULL === @$arr[$i] ) return FALSE; ++$i; } while ($i < $l); return TRUE; } // deep reference reliance function method_7(Array &$arr) { return array_keys(array_values($arr)) === array_keys($arr); } // organic (guessing + array_values) function method_8(Array &$arr) { reset($arr); if ( key($arr) !== 0 ) return FALSE; end($arr); if ( key($arr) !== count($arr)-1 ) return FALSE; return array_values($arr) === $arr; } function benchmark(Array &$methods, Array &$target, $expected){ foreach($methods as $method){ $start = microtime(true); for ($i = 0; $i < 2000; ++$i) { //$dummy = call_user_func($method, $target); if ( $method($target) !== $expected ) { echo "Method $method is disqualified for returning an incorrect result.\n"; unset($methods[array_search($method,$methods,true)]); $i = 0; break; } } if ( $i != 0 ) { $end = microtime(true); echo "Time taken with $method = ".round(($end-$start)*1000.0,3)."ms\n"; } } } $true_targets = [ 'Giant array' => range(0, 500), 'Tiny array' => range(0, 20), ]; $g = range(0,10); unset($g[0]); $false_targets = [ 'Large array 1' => range(0, 100) + ['a'=>'a'] + range(101, 200), 'Large array 2' => ['a'=>'a'] + range(0, 200), 'Tiny array' => range(0, 10) + ['a'=>'a'] + range(11, 20), 'Gotcha array' => $g, ]; $methods = [ 'method_1', 'method_3', 'method_4', 'method_5', 'method_6', 'method_7', 'method_8' ]; foreach($false_targets as $targetName => $target){ echo "==== Benchmark using $targetName expecing FALSE ====\n"; benchmark($methods, $target, false); echo "\n"; } foreach($true_targets as $targetName => $target){ echo "==== Benchmark using $targetName expecting TRUE ====\n"; benchmark($methods, $target, true); echo "\n"; }

Abusive script

This script was stopped while abusing our resources

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)
7.4.90.0160.31916.63
7.4.80.0140.22716.51
7.4.70.0150.23216.82
7.4.60.0160.21516.75
7.4.50.0170.27516.43
7.4.40.0190.21016.46
7.4.30.0070.23416.69
7.4.20.0130.24916.32
7.4.10.0190.22016.69
7.4.00.0230.21216.45
7.3.210.0070.28216.41
7.3.200.0130.26016.53
7.3.190.0210.29016.66
7.3.180.0070.24816.66
7.3.170.0060.26016.54
7.3.160.0130.24116.69
7.3.150.0120.26916.69
7.3.140.0130.25316.77
7.3.130.0100.24516.54
7.3.120.0130.25516.55
7.3.110.0160.24016.41
7.3.100.0130.28216.48
7.3.90.0130.26116.75
7.3.80.0000.26416.63
7.3.70.0060.39116.62
7.3.60.0250.24916.54
7.3.50.0060.29716.51
7.3.40.0130.37116.36
7.3.30.0160.23716.57
7.3.20.0130.25316.60
7.3.10.0080.25516.44
7.3.00.0120.28716.28
7.2.330.0100.28616.97
7.2.320.0190.26916.89
7.2.310.0100.26817.00
7.2.300.0000.29416.65
7.2.290.0100.29716.89
7.2.280.0030.27216.88
7.2.270.0000.27316.78
7.2.260.0130.27017.02
7.2.250.0130.27716.89
7.2.240.0100.26316.65
7.2.230.0070.24916.93
7.2.220.0100.36116.68
7.2.210.0100.25316.95
7.2.200.0130.39116.62
7.2.190.0100.25416.75
7.2.180.0160.25816.67
7.2.170.0130.27516.80
7.2.160.0100.28416.78
7.2.150.0130.28716.91
7.2.140.0070.27116.82
7.2.130.0150.23716.59
7.2.120.0120.24316.66
7.2.110.0120.25016.66
7.2.100.0130.23516.54
7.2.90.0200.25516.73
7.2.80.0140.26616.69
7.2.70.0160.24216.75
7.2.60.0150.26516.80
7.2.50.0130.25516.70
7.2.40.0130.28216.80
7.2.30.0180.23516.65
7.2.20.0210.28116.57
7.2.10.0220.27517.35
7.2.00.0160.31217.38
7.1.330.0170.34216.00
7.1.320.0270.33915.64
7.1.310.0500.36815.92
7.1.300.0270.33215.71
7.1.290.0190.32816.02
7.1.280.0550.41615.73
7.1.270.0100.33515.77
7.1.260.0160.34215.76
7.1.250.0240.46215.67
7.1.240.0200.42015.90
7.1.230.0230.34315.68
7.1.220.0340.31415.69
7.1.210.0230.32315.79
7.1.200.0290.36415.67
7.1.190.0380.39115.66
7.1.180.0320.34015.66
7.1.170.0250.32415.70
7.1.160.0430.49015.73
7.1.150.0260.36115.85
7.1.140.0320.38015.78
7.1.130.0290.36916.15
7.1.120.0260.43716.42
7.1.110.0510.40615.97
7.1.100.0270.43216.21
7.1.90.0210.41015.98
7.1.80.0290.41016.09
7.1.70.0200.35515.44
7.1.60.0290.35924.73
7.1.50.0470.43924.49
7.1.40.0250.33824.31
7.1.30.0310.32224.40
7.1.20.0330.40924.26
7.1.10.0280.44915.49
7.1.00.0250.37515.47
7.0.330.0290.49215.44
7.0.320.0260.34915.39
7.0.310.0300.32715.48
7.0.300.0290.46015.42
7.0.290.0220.48115.48
7.0.280.0260.38015.55
7.0.270.0230.43115.36
7.0.260.0400.35015.44
7.0.250.0450.49515.59
7.0.240.0310.38015.48
7.0.230.0330.35915.50
7.0.220.0690.43715.71
7.0.210.0210.53015.52
7.0.200.0230.34915.41
7.0.190.0250.48415.41
7.0.180.0200.35415.63
7.0.170.0230.33515.55
7.0.160.0200.35815.67
7.0.150.0270.37915.52
7.0.140.0230.33215.57
7.0.130.0190.55015.33
7.0.120.0260.41915.52
7.0.110.0130.36015.46
7.0.100.0260.34615.34
7.0.90.0270.43215.57
7.0.80.0170.33815.45
7.0.70.0190.36315.45
7.0.60.0290.35715.55
7.0.50.0130.39315.46
5.6.400.0262.20016.45
5.6.390.0642.13916.74
5.6.380.0132.42516.23
5.6.370.0261.95916.47
5.6.360.0262.29816.38
5.6.350.0162.36016.36
5.6.340.0362.36516.30
5.6.330.0282.33816.37
5.6.320.0482.06316.65
5.6.310.0192.06916.47
5.6.300.0332.11016.29
5.6.290.0202.43316.66
5.6.280.0292.08316.47
5.6.270.0522.18716.48
5.6.260.0322.09816.52
5.6.250.0202.33616.45
5.6.240.0232.06316.52
5.6.230.0132.16116.41
5.6.220.0462.35616.45
5.6.210.0202.13916.37
5.6.200.0282.01716.51

preferences:
15.55 ms | 403 KiB | 5 Q