Created
January 9, 2017 15:35
-
-
Save georgiana-gligor/78bf61c32990aeb645c21ad1c06666ec to your computer and use it in GitHub Desktop.
Performance of null checking
This file contains 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 | |
define('DEFAULT_ITERATIONS', 1000000); | |
$myNull = null; | |
$options = getopt('n:'); | |
$iterations = isset($options['n']) ? intval($options['n']) : DEFAULT_ITERATIONS; | |
logLine('iterations = ' . $iterations); | |
logLine('>>> Checking `is_null`'); | |
$startTime = lap(); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (is_null($myNull)) { | |
continue; | |
} | |
} | |
$endTime = lap(); | |
logDuration($startTime, $endTime); | |
logLine('>>> Checking `null !== `'); | |
$startTime = lap(); | |
for ($i = 0; $i < $iterations; $i++) { | |
if (null !== $myNull) { | |
continue; | |
} | |
} | |
$endTime = lap(); | |
logDuration($startTime, $endTime); | |
function lap() { | |
return microtime(true); | |
} | |
function logDuration($start, $end) { | |
$duration = $end - $start; | |
$message = sprintf('Duration: %f', $duration); | |
logLine($message); | |
} | |
function logLine($message) { | |
echo $message . PHP_EOL; | |
} |
This file contains 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
iterations = 5 | |
>>> Checking `is_null` | |
Duration: 0.000007 | |
>>> Checking `null !== ` | |
Duration: 0.000004 |
This file contains 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
iterations = 5000000 | |
>>> Checking `is_null` | |
Duration: 0.089797 | |
>>> Checking `null !== ` | |
Duration: 0.085138 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment