Skip to content

Instantly share code, notes, and snippets.

@shanethehat
Last active December 15, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanethehat/5267549 to your computer and use it in GitHub Desktop.
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.
<?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