Last active
December 15, 2015 13:29
-
-
Save shanethehat/5267549 to your computer and use it in GitHub Desktop.
Demonstration of the speed difference between calling is_null and using a comparison operator.
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 | |
function runTestFunction($name, Callable $function) { | |
$startTime = microtime(true); | |
for ($i = 0; $i < 10000000; $i++) { | |
$function(); | |
} | |
$endTime = microtime(true); | |
echo $name . ' took ' . ($endTime - $startTime); | |
echo PHP_EOL; | |
} | |
function testNullWithFunction() { | |
$testValue = null; | |
is_null($testValue); | |
} | |
function testNotNullWithFunction() { | |
$testValue = 'a'; | |
is_null($testValue); | |
} | |
function testNullWithoutFunction() { | |
$testValue = null; | |
$testValue === null; | |
} | |
function testNotNullWithoutFunction() { | |
$testValue = 'a'; | |
$testValue === null; | |
} | |
runTestFunction('NullWithFunction', 'testNullWithFunction'); | |
runTestFunction('NotNullWithFunction', 'testNotNullWithFunction'); | |
runTestFunction('NullWithComparison', 'testNullWithoutFunction'); | |
runTestFunction('NotNullWithComparison', 'testNotNullWithoutFunction'); | |
// NullWithFunction took 16.489717960358 | |
// NotNullWithFunction took 14.222808837891 | |
// NullWithComparison took 9.0285971164703 | |
// NotNullWithComparison took 9.2056560516357 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment