<?php function testCheck( int $numRounds, array $testArr, $input ) { $start = hrtime( true ); for ( $i = 0; $i < $numRounds; $i++ ) { if ( is_float( $input ) ) { $input = (int)$input; } if ( isset( $testArr[$input] ) ) { $_ = $testArr[$input]; } else { $_ = 42; } } return hrtime( true ) - $start; } function testSilence( int $numRounds, array $testArr, $input ) { $start = hrtime( true ); for ( $i = 0; $i < $numRounds; $i++ ) { if ( @isset( $testArr[$input] ) ) { $_ = @$testArr[$input]; } else { $_ = 42; } } return hrtime( true ) - $start; } $num = 1000000; $inputs = [ 1, true, 'foobar', 1.3 ]; $arrays = [ 'unset' => [], 'set' => [ 1 => 42 ] ]; foreach ( $arrays as $arrDesc => $arr ) { foreach ( $inputs as $input ) { echo "Running with array=$arrDesc and input $input", PHP_EOL; echo "Check: ", testCheck( $num, $arr, $input ) / 10**6, " ms", PHP_EOL; echo "Silence: ", testSilence( $num, $arr, $input ) / 10**6, " ms", PHP_EOL; } }
You have javascript disabled. You will not be able to edit any code.
This script was stopped while abusing our resources