Last active
August 29, 2015 13:57
-
-
Save bwg/9687806 to your computer and use it in GitHub Desktop.
php null showdown
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.6.11 | |
-------------------------------------------------- | |
Testing null over 100,000 iterations: | |
control 16.100883483887 | |
!$value 22.799968719482 | |
isset($value) 21.697998046875 | |
null !== $value 22.647142410278 | |
!empty($value) 22.5830078125 | |
-------------------------------------------------- | |
Testing empty string over 100,000 iterations: | |
control 15.342950820923 | |
!$value 24.483919143677 | |
isset($value) 21.049022674561 | |
null !== $value 23.370981216431 | |
!empty($value) 23.261070251465 | |
-------------------------------------------------- | |
Testing string value over 100,000 iterations: | |
control 14.435052871704 | |
!$value 24.092197418213 | |
isset($value) 21.024942398071 | |
null !== $value 23.869037628174 | |
!empty($value) 22.725105285645 | |
-------------------------------------------------- | |
Testing empty array over 100,000 iterations: | |
control 14.801979064941 | |
!$value 24.636030197144 | |
isset($value) 22.125005722046 | |
null !== $value 25.100946426392 | |
!empty($value) 23.957014083862 | |
-------------------------------------------------- | |
Testing indexed array(3) over 100,000 iterations: | |
control 15.141010284424 | |
!$value 22.420883178711 | |
isset($value) 24.151086807251 | |
null !== $value 25.124073028564 | |
!empty($value) 24.703979492188 | |
-------------------------------------------------- | |
Testing assoc. array(3) over 100,000 iterations: | |
control 15.416145324707 | |
!$value 24.161100387573 | |
isset($value) 20.823001861572 | |
null !== $value 23.271083831787 | |
!empty($value) 23.849010467529 | |
---------------------------------------------------------------------- | |
average (ms) total (ms) delta (ms) | |
---------------------------------------------------------------------- | |
control 15.206336975098 91.238021850586 0 | |
falsy 23.765683174133 142.5940990448 51.356077194214 | |
isset 21.811842918396 130.87105751038 39.63303565979 | |
null 23.897210756938 143.38326454163 52.14524269104 | |
empty 23.513197898865 141.07918739319 49.841165542603 |
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; | |
$values = [ | |
'null' => null, | |
'empty string' => '', | |
'string value' => 'string', | |
'empty array' => [], | |
'indexed array(3)' => [1,2,3], | |
'assoc. array(3)' => ['one' => 1, 'two' => 2, 'three' => 3] | |
]; | |
$totals = [ | |
'control' => 0, | |
'falsy' => 0, | |
'isset' => 0, | |
'null' => 0, | |
'empty' => 0, | |
]; | |
echo "PHP Version ".phpversion()."\n\n"; | |
foreach ($values as $name => $value) { | |
echo str_repeat('-', 50)."\n"; | |
echo "Testing {$name} over ".number_format($iterations)." iterations:\n\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['control'] += $duration; | |
echo str_pad('control', 35)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (!($value)) {} | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['falsy'] += $duration; | |
echo str_pad('!$value', 35)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (isset($value)) {} | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['isset'] += $duration; | |
echo str_pad('isset($value)', 35)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (null !== $value) {} | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['null'] += $duration; | |
echo str_pad('null !== $value', 35)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (!empty($value)) {} | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['empty'] += $duration; | |
echo str_pad('!empty($value)', 35)."{$duration}\n"; | |
//----------------------------------------------------------------------------- | |
} | |
echo "\n".str_repeat('-', 70)."\n"; | |
echo str_pad('', 15); | |
echo str_pad('average (ms)', 20); | |
echo str_pad('total (ms)', 20); | |
echo str_pad('delta (ms)', 20)."\n"; | |
echo str_repeat('-', 70)."\n"; | |
$control = $totals['control']; | |
foreach ($totals as $name => $total) { | |
echo str_pad($name, 15); | |
echo str_pad($total/count($values), 20); | |
echo str_pad($total, 20); | |
echo str_pad($total - $control, 20)."\n"; | |
} | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment