3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * PHP Array key exists */ $n = 1000000; // First a test with a empty array $array = array(); $time_start = microtime(true); $i = 0; while($i < $n){ $devnull = isset($array[$i++]); //var_dump($devnull); } $time_end = microtime(true); $time_while1= $time_end-$time_start; echo number_format($time_while1, 3, '.', '') ." seconds - isset(array[i]) on empty array \n"; $time_start = microtime(true); $i = 0; while($i < $n){ $devnull = array_key_exists($i++, $array); //var_dump($devnull); } $time_end = microtime(true); $time_while1= $time_end-$time_start; echo number_format($time_while1, 3, '.', '') ." seconds - array_key_exists(array,i) on empty array \n"; $time_start = microtime(true); $i = 0; while($i < $n){ $devnull = (bool)@$array[$i++]; //var_dump($devnull); } $time_end = microtime(true); $time_while1= $time_end-$time_start; echo number_format($time_while1, 3, '.', '') ." seconds - cast array[i] on empty array \n"; // Create test array $i = 0; $array = array(); while($i < $n) { $array[$i++] = true; } $time_start = microtime(true); $i = 0; while($i < $n){ $devnull = isset($array[$i++]); //var_dump($devnull); } $time_end = microtime(true); $time_while1= $time_end-$time_start; echo number_format($time_while1, 3, '.', '') ." seconds - isset(array[i]) on full array \n"; $time_start = microtime(true); $i = 0; while($i < $n){ $devnull = array_key_exists($i++, $array); //var_dump($devnull); } $time_end = microtime(true); $time_while1= $time_end-$time_start; echo number_format($time_while1, 3, '.', '') ." seconds - array_key_exists(array,i) on full array \n"; $time_start = microtime(true); $i = 0; while($i < $n){ $devnull = (bool)@$array[$i++]; //var_dump($devnull); } $time_end = microtime(true); $time_while1= $time_end-$time_start; echo number_format($time_while1, 3, '.', '') ." seconds - cast array[i] on full array \n"; ?>

preferences:
29.74 ms | 402 KiB | 5 Q