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

preferences:
26.31 ms | 405 KiB | 5 Q