The fact that type save comparisons (ala ===) are faster in PHP than the normal comparison operator (ala ==). The reason for this is simply, that PHPs loosly-typed-ness-auto-cast-code is not even touched with ===, AFAIK. So, if you did not know, yet:
$foo = 0; $bar = "0";
if ( $foo == $bar )
// ...is slower than
$foo = 0; $bar = 0;
if ( $foo == $bar )
// ...wich is again slower than
$foo = 0; $bar = 0;
if ( $foo === $bar )
// ...But, this should not be the topic of this article, it's just an interessting pre-condition to know and the reason why I started to code strictly type save in PHP a longer time ago. Code like
$foo = array();
if ( count( $foo ) === 0 )
// ...was the goal of this behavioural change. But, as Kore pointed out while we were coding together, it also led to funny code construct like:
if ( isset( $foo ) === true )This looks kinda funny, doesn't it? But as I noticed recently, I more and more tend to code this way. So, the valid question was, does this have any effect on speed here? Positive or negative? And my little benchmark (code) revealed: There is a slide speed improvement here, too. So I basically don't care that I started it by accident. ;)
P.S. I love PHP 5.3! ;) Many congratulations and a lot of luck to Johannes as well as a huge ton of thanks to Illia!
If you liked this blog post or learned something, please consider using flattr to contribute back: .
Fields with bold names are mandatory.
Edward Z. Yang
For me:
Link to commentisset()
Average time: 1.7711260318756E-5
isset() == true
Average time: 1.8002052307129E-5
isset() === true
Average time: 1.7838664054871E-5
The change really is not statistically significant, and even if it does give a performance boost any optimizer worth its salt should perform it (just like the $i++ to ++$i conversion).
Jonathan Street
I've recently covered benchmarks in some detail on my site. Including what I feel is necessary to convincingly demonstrate a difference. I probably take it too far but you might find it worth the read.
Link to commenthttp://torrentialwebdev.com/blog/archives/118-Better-Benchmarks.html
Chuck Burgess
I've been coding this way too, nearly:
Link to comment- if ( true === isset($foo) )
vs
- if ( isset($foo) === true )
to avoid the kind of "something's value unexpectedly resolved to FALSE" problems with just using something like
- if ( $foo )
as well as putting my conditional pieces "backward" to common practice, to help avoid "accidental assignment" typo errors. I know those would be more common with "==" than with "===", but it's a habit-by-convention for me.
It is nice to know that all the seemingly-extra steps I have in there
- if ( true === isset($foo) )
vs
- if ( isset($foo) )
aren't slowing it down significantly. I was worried it did, though a slowdown would have to have been significant (and proven to me) to make me change my ways.
Sara Golemon
Another excellent examples of why benchmarks shouldn't be trusted.
Link to commentif (isset($foo) === true)
is doing MORE work than
if (isset($foo))
For the same reason that
if ((isset($foo) === true) === true)
is doing even more work
ISSET_ISEMPTY $0 !0
IS_IDENTICAL $1 $0 true
JMPZ $1 -123
versus
ISSET_ISEMPTY $0 !0
JMPZ $0 -123
The JMPZ instruction is working on the same (effective) value in both examples, but the former had to do an extra comparison before hand.
I do like the explicitness inherent in the code though, but that's a stylectic thing, not a performance issue.
Mgccl
You might want to try that on a
Link to comment1. Stable server
2. Run one test each time because on my server, it seems any code in the front part of the script always run faster.
Thomas Koch
@Mgccl:
Link to commentTake care, that all the variables you use in your benchmark are initialized above the benchmark code. I encountered the same phenomenon with a benchmark code like:
$start = microtime();
(...benchmark code...)
$stop = microtime();
echo 'first benchmark: ',$stop-$start;
$start = microtime();
(...another benchmark code...)
$stop = microtime();
echo 'second benchmark: ',$stop-$start;
The first benchmark will always be a little slower, because the two variables $start and $stop needs to be initialized, while they are already initialized when running the second benchmark.
Tom
You should write maintainable code first, and optimized code second. Your code sounds like it would be horrible to read ...
Link to commentDude
You said: "any optimizer worth its salt should perform it (just like the $i++ to ++$i conversion)."
Link to comment$i++ and $i++ are not equal. Example:
$test = array('A','B','C','D');
$i = 0;
echo $test[++$i];
echo $test[$i++];
The output should be "BC";