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
Output for 7.4.28
tryGetValue1: 0.0039 tryGetValue2: 0.0095 tryGetValue3: 0.0044 tryGetValueNotNull1: 0.0087 tryGetValueNotNull2: 0.0038
Output for 7.1.7
tryGetValue1: 0.0078 tryGetValue2: 0.0142 tryGetValue3: 0.0076 tryGetValueNotNull1: 0.0124 tryGetValueNotNull2: 0.0059
Output for 7.1.6
tryGetValue1: 0.0126 tryGetValue2: 0.0218 tryGetValue3: 0.0109 tryGetValueNotNull1: 0.0171 tryGetValueNotNull2: 0.0084
Output for 7.1.5
tryGetValue1: 0.0113 tryGetValue2: 0.0198 tryGetValue3: 0.0097 tryGetValueNotNull1: 0.0156 tryGetValueNotNull2: 0.0075
Output for 7.1.0
tryGetValue1: 0.0097 tryGetValue2: 0.0173 tryGetValue3: 0.0088 tryGetValueNotNull1: 0.0137 tryGetValueNotNull2: 0.0069
Output for 7.0.20
tryGetValue1: 0.0072 tryGetValue2: 0.0129 tryGetValue3: 0.0076 tryGetValueNotNull1: 0.0119 tryGetValueNotNull2: 0.0082
Output for 7.0.10
tryGetValue1: 0.0068 tryGetValue2: 0.0117 tryGetValue3: 0.0064 tryGetValueNotNull1: 0.0101 tryGetValueNotNull2: 0.0057
Output for 7.0.9
tryGetValue1: 0.0111 tryGetValue2: 0.0170 tryGetValue3: 0.0090 tryGetValueNotNull1: 0.0140 tryGetValueNotNull2: 0.0067
Output for 7.0.8
tryGetValue1: 0.0077 tryGetValue2: 0.0125 tryGetValue3: 0.0063 tryGetValueNotNull1: 0.0104 tryGetValueNotNull2: 0.0054
Output for 7.0.7
tryGetValue1: 0.0098 tryGetValue2: 0.0171 tryGetValue3: 0.0088 tryGetValueNotNull1: 0.0142 tryGetValueNotNull2: 0.0070
Output for 7.0.6
tryGetValue1: 0.0065 tryGetValue2: 0.0127 tryGetValue3: 0.0064 tryGetValueNotNull1: 0.0110 tryGetValueNotNull2: 0.0053
Output for 7.0.5
tryGetValue1: 0.0079 tryGetValue2: 0.0140 tryGetValue3: 0.0075 tryGetValueNotNull1: 0.0118 tryGetValueNotNull2: 0.0062
Output for 7.0.4
tryGetValue1: 0.0078 tryGetValue2: 0.0143 tryGetValue3: 0.0071 tryGetValueNotNull1: 0.0098 tryGetValueNotNull2: 0.0054
Output for 7.0.3
tryGetValue1: 0.0079 tryGetValue2: 0.0147 tryGetValue3: 0.0075 tryGetValueNotNull1: 0.0124 tryGetValueNotNull2: 0.0061
Output for 7.0.2
tryGetValue1: 0.0081 tryGetValue2: 0.0114 tryGetValue3: 0.0062 tryGetValueNotNull1: 0.0103 tryGetValueNotNull2: 0.0052
Output for 7.0.1
tryGetValue1: 0.0067 tryGetValue2: 0.0127 tryGetValue3: 0.0063 tryGetValueNotNull1: 0.0106 tryGetValueNotNull2: 0.0053
Output for 7.0.0
tryGetValue1: 0.0078 tryGetValue2: 0.0144 tryGetValue3: 0.0078 tryGetValueNotNull1: 0.0127 tryGetValueNotNull2: 0.0064
Output for 5.6.28
tryGetValue1: 0.0197 tryGetValue2: 0.0354 tryGetValue3: 0.0167 tryGetValueNotNull1: 0.0302 tryGetValueNotNull2: 0.0149
Output for 5.6.25
tryGetValue1: 0.0181 tryGetValue2: 0.0395 tryGetValue3: 0.0215 tryGetValueNotNull1: 0.0334 tryGetValueNotNull2: 0.0166
Output for 5.6.24
tryGetValue1: 0.0175 tryGetValue2: 0.0346 tryGetValue3: 0.0185 tryGetValueNotNull1: 0.0320 tryGetValueNotNull2: 0.0163
Output for 5.6.23
tryGetValue1: 0.0220 tryGetValue2: 0.0392 tryGetValue3: 0.0188 tryGetValueNotNull1: 0.0327 tryGetValueNotNull2: 0.0164
Output for 5.6.22
tryGetValue1: 0.0257 tryGetValue2: 0.0537 tryGetValue3: 0.0202 tryGetValueNotNull1: 0.0359 tryGetValueNotNull2: 0.0190
Output for 5.6.21
tryGetValue1: 0.0252 tryGetValue2: 0.0439 tryGetValue3: 0.0220 tryGetValueNotNull1: 0.0349 tryGetValueNotNull2: 0.0169
Output for 5.6.20
tryGetValue1: 0.0239 tryGetValue2: 0.0425 tryGetValue3: 0.0213 tryGetValueNotNull1: 0.0337 tryGetValueNotNull2: 0.0163
Output for 5.6.19
tryGetValue1: 0.0210 tryGetValue2: 0.0402 tryGetValue3: 0.0190 tryGetValueNotNull1: 0.0319 tryGetValueNotNull2: 0.0163
Output for 5.6.18
tryGetValue1: 0.0267 tryGetValue2: 0.0466 tryGetValue3: 0.0232 tryGetValueNotNull1: 0.0361 tryGetValueNotNull2: 0.0171
Output for 5.6.17
tryGetValue1: 0.0194 tryGetValue2: 0.0367 tryGetValue3: 0.0186 tryGetValueNotNull1: 0.0318 tryGetValueNotNull2: 0.0172
Output for 5.6.16
tryGetValue1: 0.0228 tryGetValue2: 0.0405 tryGetValue3: 0.0196 tryGetValueNotNull1: 0.0347 tryGetValueNotNull2: 0.0170
Output for 5.6.15
tryGetValue1: 0.0269 tryGetValue2: 0.0464 tryGetValue3: 0.0219 tryGetValueNotNull1: 0.0374 tryGetValueNotNull2: 0.0189
Output for 5.6.14
tryGetValue1: 0.0243 tryGetValue2: 0.0425 tryGetValue3: 0.0224 tryGetValueNotNull1: 0.0379 tryGetValueNotNull2: 0.0178
Output for 5.6.13
tryGetValue1: 0.0247 tryGetValue2: 0.0412 tryGetValue3: 0.0198 tryGetValueNotNull1: 0.0336 tryGetValueNotNull2: 0.0161
Output for 5.6.12
tryGetValue1: 0.0258 tryGetValue2: 0.0447 tryGetValue3: 0.0216 tryGetValueNotNull1: 0.0351 tryGetValueNotNull2: 0.0171
Output for 5.6.11
tryGetValue1: 0.0189 tryGetValue2: 0.0356 tryGetValue3: 0.0186 tryGetValueNotNull1: 0.0322 tryGetValueNotNull2: 0.0160
Output for 5.6.10
tryGetValue1: 0.0222 tryGetValue2: 0.0380 tryGetValue3: 0.0192 tryGetValueNotNull1: 0.0321 tryGetValueNotNull2: 0.0163
Output for 5.6.9
tryGetValue1: 0.0177 tryGetValue2: 0.0360 tryGetValue3: 0.0188 tryGetValueNotNull1: 0.0332 tryGetValueNotNull2: 0.0160
Output for 5.6.8
tryGetValue1: 0.0192 tryGetValue2: 0.0362 tryGetValue3: 0.0190 tryGetValueNotNull1: 0.0322 tryGetValueNotNull2: 0.0162
Output for 5.6.7
tryGetValue1: 0.0207 tryGetValue2: 0.0420 tryGetValue3: 0.0208 tryGetValueNotNull1: 0.0377 tryGetValueNotNull2: 0.0181
Output for 5.6.6
tryGetValue1: 0.0221 tryGetValue2: 0.0408 tryGetValue3: 0.0199 tryGetValueNotNull1: 0.0325 tryGetValueNotNull2: 0.0161
Output for 5.6.5
tryGetValue1: 0.0281 tryGetValue2: 0.0440 tryGetValue3: 0.0187 tryGetValueNotNull1: 0.0319 tryGetValueNotNull2: 0.0164
Output for 5.6.4
tryGetValue1: 0.0207 tryGetValue2: 0.0383 tryGetValue3: 0.0187 tryGetValueNotNull1: 0.0327 tryGetValueNotNull2: 0.0165
Output for 5.6.3
tryGetValue1: 0.0287 tryGetValue2: 0.0458 tryGetValue3: 0.0202 tryGetValueNotNull1: 0.0326 tryGetValueNotNull2: 0.0162
Output for 5.6.2
tryGetValue1: 0.0307 tryGetValue2: 0.0477 tryGetValue3: 0.0253 tryGetValueNotNull1: 0.0399 tryGetValueNotNull2: 0.0208
Output for 5.6.1
tryGetValue1: 0.0235 tryGetValue2: 0.0421 tryGetValue3: 0.0208 tryGetValueNotNull1: 0.0328 tryGetValueNotNull2: 0.0161
Output for 5.6.0
tryGetValue1: 0.0308 tryGetValue2: 0.0461 tryGetValue3: 0.0204 tryGetValueNotNull1: 0.0324 tryGetValueNotNull2: 0.0161
Output for 5.5.38
tryGetValue1: 0.0273 tryGetValue2: 0.0465 tryGetValue3: 0.0222 tryGetValueNotNull1: 0.0341 tryGetValueNotNull2: 0.0168
Output for 5.5.37
tryGetValue1: 0.0247 tryGetValue2: 0.0441 tryGetValue3: 0.0217 tryGetValueNotNull1: 0.0350 tryGetValueNotNull2: 0.0167
Output for 5.5.36
tryGetValue1: 0.0326 tryGetValue2: 0.0556 tryGetValue3: 0.0239 tryGetValueNotNull1: 0.0414 tryGetValueNotNull2: 0.0192
Output for 5.5.35
tryGetValue1: 0.0284 tryGetValue2: 0.0443 tryGetValue3: 0.0201 tryGetValueNotNull1: 0.0333 tryGetValueNotNull2: 0.0172
Output for 5.5.34
tryGetValue1: 0.0259 tryGetValue2: 0.0417 tryGetValue3: 0.0209 tryGetValueNotNull1: 0.0332 tryGetValueNotNull2: 0.0169
Output for 5.5.33
tryGetValue1: 0.0295 tryGetValue2: 0.0515 tryGetValue3: 0.0238 tryGetValueNotNull1: 0.0412 tryGetValueNotNull2: 0.0203
Output for 5.5.32
tryGetValue1: 0.0238 tryGetValue2: 0.0394 tryGetValue3: 0.0193 tryGetValueNotNull1: 0.0326 tryGetValueNotNull2: 0.0165
Output for 5.5.31
tryGetValue1: 0.0262 tryGetValue2: 0.0446 tryGetValue3: 0.0194 tryGetValueNotNull1: 0.0337 tryGetValueNotNull2: 0.0165
Output for 5.5.30
tryGetValue1: 0.0247 tryGetValue2: 0.0453 tryGetValue3: 0.0216 tryGetValueNotNull1: 0.0350 tryGetValueNotNull2: 0.0164
Output for 5.5.29
tryGetValue1: 0.0247 tryGetValue2: 0.0430 tryGetValue3: 0.0213 tryGetValueNotNull1: 0.0387 tryGetValueNotNull2: 0.0191
Output for 5.5.28
tryGetValue1: 0.0200 tryGetValue2: 0.0361 tryGetValue3: 0.0190 tryGetValueNotNull1: 0.0326 tryGetValueNotNull2: 0.0163
Output for 5.5.27
tryGetValue1: 0.0181 tryGetValue2: 0.0366 tryGetValue3: 0.0193 tryGetValueNotNull1: 0.0329 tryGetValueNotNull2: 0.0167
Output for 5.5.26
tryGetValue1: 0.0271 tryGetValue2: 0.0453 tryGetValue3: 0.0193 tryGetValueNotNull1: 0.0327 tryGetValueNotNull2: 0.0167
Output for 5.5.25
tryGetValue1: 0.0202 tryGetValue2: 0.0400 tryGetValue3: 0.0192 tryGetValueNotNull1: 0.0347 tryGetValueNotNull2: 0.0166
Output for 5.5.24
tryGetValue1: 0.0242 tryGetValue2: 0.0438 tryGetValue3: 0.0227 tryGetValueNotNull1: 0.0414 tryGetValueNotNull2: 0.0206
Output for 5.5.23
tryGetValue1: 0.0192 tryGetValue2: 0.0393 tryGetValue3: 0.0198 tryGetValueNotNull1: 0.0387 tryGetValueNotNull2: 0.0163
Output for 5.5.22
tryGetValue1: 0.0245 tryGetValue2: 0.0449 tryGetValue3: 0.0227 tryGetValueNotNull1: 0.0387 tryGetValueNotNull2: 0.0184
Output for 5.5.21
tryGetValue1: 0.0333 tryGetValue2: 0.0471 tryGetValue3: 0.0196 tryGetValueNotNull1: 0.0328 tryGetValueNotNull2: 0.0167
Output for 5.5.20
tryGetValue1: 0.0277 tryGetValue2: 0.0472 tryGetValue3: 0.0230 tryGetValueNotNull1: 0.0362 tryGetValueNotNull2: 0.0171
Output for 5.5.19
tryGetValue1: 0.0292 tryGetValue2: 0.0474 tryGetValue3: 0.0197 tryGetValueNotNull1: 0.0336 tryGetValueNotNull2: 0.0175
Output for 5.5.18
tryGetValue1: 0.0218 tryGetValue2: 0.0385 tryGetValue3: 0.0194 tryGetValueNotNull1: 0.0321 tryGetValueNotNull2: 0.0163
Output for 5.5.16
tryGetValue1: 0.0210 tryGetValue2: 0.0389 tryGetValue3: 0.0196 tryGetValueNotNull1: 0.0334 tryGetValueNotNull2: 0.0170
Output for 5.5.15
tryGetValue1: 0.0293 tryGetValue2: 0.0423 tryGetValue3: 0.0203 tryGetValueNotNull1: 0.0330 tryGetValueNotNull2: 0.0169
Output for 5.5.14
tryGetValue1: 0.0244 tryGetValue2: 0.0447 tryGetValue3: 0.0231 tryGetValueNotNull1: 0.0397 tryGetValueNotNull2: 0.0208
Output for 5.5.13
tryGetValue1: 0.0298 tryGetValue2: 0.0477 tryGetValue3: 0.0232 tryGetValueNotNull1: 0.0358 tryGetValueNotNull2: 0.0177
Output for 5.5.12
tryGetValue1: 0.0198 tryGetValue2: 0.0361 tryGetValue3: 0.0189 tryGetValueNotNull1: 0.0329 tryGetValueNotNull2: 0.0168
Output for 5.5.11
tryGetValue1: 0.0205 tryGetValue2: 0.0396 tryGetValue3: 0.0194 tryGetValueNotNull1: 0.0326 tryGetValueNotNull2: 0.0165
Output for 5.5.10
tryGetValue1: 0.0236 tryGetValue2: 0.0433 tryGetValue3: 0.0210 tryGetValueNotNull1: 0.0341 tryGetValueNotNull2: 0.0163
Output for 5.5.9
tryGetValue1: 0.0376 tryGetValue2: 0.0569 tryGetValue3: 0.0261 tryGetValueNotNull1: 0.0413 tryGetValueNotNull2: 0.0186
Output for 5.5.8
tryGetValue1: 0.0225 tryGetValue2: 0.0405 tryGetValue3: 0.0190 tryGetValueNotNull1: 0.0331 tryGetValueNotNull2: 0.0167
Output for 5.5.7
tryGetValue1: 0.0225 tryGetValue2: 0.0408 tryGetValue3: 0.0191 tryGetValueNotNull1: 0.0322 tryGetValueNotNull2: 0.0168
Output for 5.5.6
tryGetValue1: 0.0222 tryGetValue2: 0.0380 tryGetValue3: 0.0195 tryGetValueNotNull1: 0.0327 tryGetValueNotNull2: 0.0165
Output for 5.5.5
tryGetValue1: 0.0251 tryGetValue2: 0.0449 tryGetValue3: 0.0218 tryGetValueNotNull1: 0.0349 tryGetValueNotNull2: 0.0169
Output for 5.5.4
tryGetValue1: 0.0187 tryGetValue2: 0.0359 tryGetValue3: 0.0193 tryGetValueNotNull1: 0.0330 tryGetValueNotNull2: 0.0166
Output for 5.5.3
tryGetValue1: 0.0234 tryGetValue2: 0.0430 tryGetValue3: 0.0214 tryGetValueNotNull1: 0.0339 tryGetValueNotNull2: 0.0168
Output for 5.5.2
tryGetValue1: 0.0263 tryGetValue2: 0.0443 tryGetValue3: 0.0225 tryGetValueNotNull1: 0.0335 tryGetValueNotNull2: 0.0173
Output for 5.5.1
tryGetValue1: 0.0246 tryGetValue2: 0.0445 tryGetValue3: 0.0216 tryGetValueNotNull1: 0.0336 tryGetValueNotNull2: 0.0167
Output for 5.5.0
tryGetValue1: 0.0394 tryGetValue2: 0.0522 tryGetValue3: 0.0223 tryGetValueNotNull1: 0.0333 tryGetValueNotNull2: 0.0169

preferences:
86.15 ms | 529 KiB | 5 Q