Funny speed considerations - Blog - Open Source - - php, photography and private stuff

Funny speed considerations

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: .



Add new comment

Fields with bold names are mandatory.