From the know your language department: If you can, go with the identical comparison operator ===. That’s a good approach in PHP for various reasons.
Only one example:
null === $var;
could be written as:
is_null($var);
but it is about more than 14 times slower according to some benchmarks onto those two expressions:
Iterations: 100000 (10 Runs) # | NULL | rel % | is_null() | rel % ----+----------+--------+-----------+-------- 1 | 0.02555 | 6.7 % | 0.37890 | 1482.9% 2 | 0.02458 | 6.4 % | 0.38507 | 1566.6% 3 | 0.02499 | 6.6 % | 0.37809 | 1512.8% 4 | 0.02490 | 6.4 % | 0.38658 | 1552.6% 5 | 0.02504 | 6.6 % | 0.38042 | 1519.2% 6 | 0.02453 | 6.3 % | 0.38666 | 1576.0% 7 | 0.02493 | 6.6 % | 0.37876 | 1519.5% 8 | 0.02529 | 6.6 % | 0.38502 | 1522.5% 9 | 0.02536 | 6.7 % | 0.37858 | 1492.6% 10 | 0.02459 | 6.4 % | 0.38363 | 1560.4%
This is the benchmark script:
benchmark(); function benchmark() { static $var; $runs = 100000; $rounds = 10; header('Content-Type: text/plain;'); printf("Iterations: %d (%d Runs)\n", $runs, $rounds); printf(" # | NULL | rel %% | is_null() | rel %% \n"); printf("----+----------+--------+-----------+--------\n"); for($b=0; $b < $rounds; $b++) { $start = microtime(true); for ($i = 0; is_null($var), $i < $runs; $i++); $timea = microtime(true) - $start; $start = microtime(true); for ($i = 0; null === $var, $i < $runs; $i++); $timeb = microtime(true) - $start; printf(" %2d | %01.5f | %4.1f %% | %01.5f | %6.1f%%\n", $b + 1, $timeb, ($timeb / $timea) * 100, $timea, ($timea / $timeb) * 100); } }
Awesome… just the research I needed to prove what I thought to be true.
There is also a difference between
NULL === $var
and
$var === NULL
Adding another column to this benchmark using $var === null intead of null === $var, we can see that is a little bit faster.
Interesting, but I have only a difference of 500% to 700%. Also with 100000 comparisons the difference with 40ms (here on my local machine) seems not big enough to break the semantics: it is something different to ask “is the value of $var null”, or “is a null identical to the value $var” (beside this I don’t like the yoda-comparison not very much ;)). Usually the code does a little bit more than just compare the value, so the operation, that depends on the comparison, is probably some magnitudes bigger, than the 0,00048ms.
However, I prefer the simple test with “$var”/”!$var” anyway 🙂 Most cases are covered (objects are always “true” and for example IDs usually starts with 1 and so on). But yes, a little bit confusing, that this two operations behaves so differently, although the outcome is identical.
tl;dr: If you ask me thaths micro optimization :X
Yes, and this benchmark is older, I guess PHP 5.3 something. I leave it here because it was fun and it reminds me that there is some strong fun factor in micro optimization but not much use.
Well its 2016 now and I ran the benchmark because I thought it would be fun to try.. not much difference for php7 🙂
Iterations: 100000 (10 Runs)
# | NULL | rel % | is_null() | rel %
—-+———-+——–+———–+——–
1 | 0.00214 | 104.3 % | 0.00205 | 95.8%
2 | 0.00215 | 105.4 % | 0.00204 | 94.9%
3 | 0.00214 | 104.9 % | 0.00204 | 95.3%
4 | 0.00216 | 104.9 % | 0.00206 | 95.3%
5 | 0.00214 | 104.3 % | 0.00205 | 95.8%
6 | 0.00214 | 105.3 % | 0.00203 | 94.9%
7 | 0.00219 | 107.7 % | 0.00203 | 92.9%
8 | 0.00214 | 105.1 % | 0.00204 | 95.2%
9 | 0.00233 | 113.7 % | 0.00205 | 88.0%
10 | 0.00215 | 105.9 % | 0.00203 | 94.4%
FYI
PHP 7 changed the game. The benchmark results are roughly equal, with both variants only having very slight variations from 100%.