3v4l.org

run code in 300+ PHP versions simultaneously
<?php function tryGetValue1( $array, $key, Closure $default ) { return array_key_exists($key, $array) ? $array[$key] : $default(); } function tryGetValue2( $array, $key, Closure $default ) { return ($value = @$array[$key]) !== null || array_key_exists($key, $array) ? $value : $default(); } function tryGetValue3( $array, $key, Closure $default ) { return isset($array[$key]) || array_key_exists($key, $array) ? $array[$key] : $default(); } function tryGetValueNotNull1( $array, $key, Closure $default ) { return ($value = @$array[$key]) !== null ? $value : $default(); } function tryGetValueNotNull2( $array, $key, Closure $default ) { return isset($array[$key]) ? $array[$key] : $default(); } $derp = function() { return 0; }; $k = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ); $r = array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ); $x = 0; // "spin up" any JIT beforehand for ( $i = 0; $i < 100; ++$i ) { $x &= tryGetValue1( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } for ( $i = 0; $i < 100; ++$i ) { $x &= tryGetValue2( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } for ( $i = 0; $i < 100; ++$i ) { $x &= tryGetValue3( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } for ( $i = 0; $i < 100; ++$i ) { $x &= tryGetValueNotNull1( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } for ( $i = 0; $i < 100; ++$i ) { $x &= tryGetValueNotNull2( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } $start=microtime(true); // start timer for ( $i = 0; $i < 50000; ++$i ) { $x = tryGetValue1( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } $end=microtime(true); // end timer echo 'tryGetValue1: ', substr( $end-$start, 0, 6 ), PHP_EOL; // echo results $start=microtime(true); // start timer for ( $i = 0; $i < 50000; ++$i ) { $x = tryGetValue2( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } $end=microtime(true); // end timer echo 'tryGetValue2: ', substr( $end-$start, 0, 6 ), PHP_EOL; // echo results $start=microtime(true); // start timer for ( $i = 0; $i < 50000; ++$i ) { $x = tryGetValue3( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } $end=microtime(true); // end timer echo 'tryGetValue3: ', substr( $end-$start, 0, 6 ), PHP_EOL; // echo results $start=microtime(true); // start timer for ( $i = 0; $i < 50000; ++$i ) { $x = tryGetValueNotNull1( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } $end=microtime(true); // end timer echo 'tryGetValueNotNull1: ', substr( $end-$start, 0, 6 ), PHP_EOL; // echo results $start=microtime(true); // start timer for ( $i = 0; $i < 50000; ++$i ) { $x = tryGetValueNotNull2( $r, $k[$i%10], $derp ); if ( $x > 5 ) throw new RuntimeException('The sun might be exploding.'); } $end=microtime(true); // end timer echo 'tryGetValueNotNull2: ', substr( $end-$start, 0, 6 ), PHP_EOL; // echo results

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.280.0040.03516.55
7.1.70.0070.05217.19
7.1.60.0130.08219.13
7.1.50.0030.08116.96
7.1.00.0000.12722.54
7.0.200.0000.05716.73
7.0.100.0170.09020.07
7.0.90.0100.14319.98
7.0.80.0070.10320.09
7.0.70.0070.14320.04
7.0.60.0070.09320.04
7.0.50.0030.12720.43
7.0.40.0100.10719.96
7.0.30.0100.12320.07
7.0.20.0130.11020.18
7.0.10.0200.08320.07
7.0.00.0130.13320.10
5.6.280.0070.15320.98
5.6.250.0070.17320.77
5.6.240.0070.16020.78
5.6.230.0030.19320.74
5.6.220.0000.23320.70
5.6.210.0170.20320.80
5.6.200.0000.21721.00
5.6.190.0070.20321.16
5.6.180.0000.23721.12
5.6.170.0130.17721.06
5.6.160.0100.21021.16
5.6.150.0070.22321.23
5.6.140.0070.21021.03
5.6.130.0070.21020.99
5.6.120.0070.22321.07
5.6.110.0100.18021.11
5.6.100.0100.19721.00
5.6.90.0100.16321.04
5.6.80.0070.18020.45
5.6.70.0200.17720.48
5.6.60.0100.21020.37
5.6.50.0030.21320.48
5.6.40.0070.19720.50
5.6.30.0100.20020.55
5.6.20.0130.20320.53
5.6.10.0030.21320.41
5.6.00.0070.19020.53
5.5.380.0270.20720.40
5.5.370.0070.21020.55
5.5.360.0100.25320.45
5.5.350.0100.22020.42
5.5.340.0200.20720.98
5.5.330.0200.23320.89
5.5.320.0030.21720.93
5.5.310.0100.21320.95
5.5.300.0130.20720.92
5.5.290.0130.20020.97
5.5.280.0200.17320.95
5.5.270.0000.17320.93
5.5.260.0170.21020.94
5.5.250.0130.18720.76
5.5.240.0000.21320.30
5.5.230.0130.17720.30
5.5.220.0230.19320.07
5.5.210.0000.24020.30
5.5.200.0070.22320.25
5.5.190.0230.19720.23
5.5.180.0030.18320.30
5.5.160.0100.19320.16
5.5.150.0070.22020.29
5.5.140.0100.20720.29
5.5.130.0200.22320.20
5.5.120.0030.17320.31
5.5.110.0070.19320.13
5.5.100.0030.20020.16
5.5.90.0130.25720.11
5.5.80.0170.19320.20
5.5.70.0100.17320.12
5.5.60.0070.18320.02
5.5.50.0100.20720.20
5.5.40.0100.16020.14
5.5.30.0030.19720.20
5.5.20.0070.21020.16
5.5.10.0000.20720.03
5.5.00.0200.22720.17

preferences:
31.23 ms | 403 KiB | 5 Q