Skip to content

Instantly share code, notes, and snippets.

@AD7six
Created August 15, 2013 09:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AD7six/6239605 to your computer and use it in GitHub Desktop.
Save AD7six/6239605 to your computer and use it in GitHub Desktop.
Is is_null still slow?
<?php
$null = null;
$notNull = 123;
$is_null = function($value) {
for($i = 0; $i < 1000000; $i++) {
$result = is_null($value);
}
return "is_null";
};
$equal_null = function($value) {
for($i = 0; $i < 1000000; $i++) {
$result = ($value === null);
}
return "===";
};
function bench($value, $callback) {
$start = microtime(true);
$name = $callback($value);
$end = microtime(true);
$duration = ($end - $start) * 1000;
echo var_export($value, true) . " testing for $name, took $duration ms\n";
}
bench($null, $is_null);
bench($null, $equal_null);
bench($notNull, $is_null);
bench($notNull, $equal_null);
@AD7six
Copy link
Author

AD7six commented Aug 15, 2013

-> php bench.php 
NULL testing for is_null, took 5324.5060443878 ms
NULL testing for ===, took 180.72295188904 ms
123 testing for is_null, took 5317.1510696411 ms
123 testing for ===, took 178.15494537354 ms

Yes it's still slow.

1k calls to is_null is 5ms slower than using strict equality checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment