Last active
August 29, 2015 14:03
-
-
Save bwg/8af307623e6b8ec06f2d to your computer and use it in GitHub Desktop.
is checking an array_key faster than disabling E_NOTICE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP Version 5.4.30 | |
------------------------------------------------------- | |
Testing 10 item array over 100,000 iterations: | |
isset 18.280982971191 | |
array_key_exists 96.060037612915 | |
^E_NOTICE 71.103811264038 | |
------------------------------------------------------- | |
Testing 100 item array over 100,000 iterations: | |
isset 17.678022384644 | |
array_key_exists 94.587087631226 | |
^E_NOTICE 66.627979278564 | |
------------------------------------------------------- | |
Testing 1,000 item array over 100,000 iterations: | |
isset 17.863035202026 | |
array_key_exists 95.493078231812 | |
^E_NOTICE 66.738128662109 | |
------------------------------------------------------- | |
Testing 10,000 item array over 100,000 iterations: | |
isset 17.688035964966 | |
array_key_exists 95.767021179199 | |
^E_NOTICE 61.712980270386 | |
------------------------------------------------------- | |
Testing 50,000 item array over 100,000 iterations: | |
isset 17.810821533203 | |
array_key_exists 91.847896575928 | |
^E_NOTICE 61.782121658325 | |
------------------------------------------------------- | |
Testing 100,000 item array over 100,000 iterations: | |
isset 17.742872238159 | |
array_key_exists 98.483085632324 | |
^E_NOTICE 62.968015670776 | |
------------------------------------------------------- | |
average (ms) total (ms) | |
------------------------------------------------------- | |
isset 17.843961715698 107.06377029419 | |
array_key_exists 95.373034477234 572.2382068634 | |
^E_NOTICE 65.155506134033 390.9330368042 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$iterations = 100000; | |
$sizes = [10, 100, 1000, 10000, 50000, 100000]; | |
$totals = [ | |
'isset' => 0, | |
'array_key_exists' => 0, | |
'^E_NOTICE' => 0, | |
]; | |
echo "PHP Version ".phpversion()."\n\n"; | |
foreach ($sizes as $size) { | |
$source = array_fill(0, $size, true); | |
echo str_repeat('-', 55)."\n"; | |
echo "Testing ".number_format($size)." item array over ".number_format($iterations)." iterations:\n\n"; | |
error_reporting(E_ALL); | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (isset($source['foo'])) { | |
$val = $source['foo']; | |
} | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['isset'] += $duration; | |
echo str_pad('isset', 40)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
error_reporting(E_ALL); | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (array_key_exists('foo', $source)) { | |
$val = $source['foo']; | |
} | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['array_key_exists'] += $duration; | |
echo str_pad('array_key_exists', 40)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
error_reporting(E_ALL ^ E_NOTICE); | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$val = $source['foo']; | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['^E_NOTICE'] += $duration; | |
echo str_pad('^E_NOTICE', 40)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
} | |
echo "\n".str_repeat('-', 55)."\n"; | |
echo str_pad('', 20); | |
echo str_pad('average (ms)', 20); | |
echo str_pad('total (ms)', 20)."\n"; | |
echo str_repeat('-', 55)."\n"; | |
foreach ($totals as $name => $total) { | |
echo str_pad($name, 20); | |
echo str_pad($total/count($sizes), 20); | |
echo str_pad($total, 20)."\n"; | |
} | |
echo "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment