PHP: Compare vs. Calculating (1/2)

This speed comparison test I’ve made is not of that much interest, but it shows somehow when / how optimization takes place. Those “performance” test are most likely not that valuable for optimizations but to better know the language.

I wanted to evaluate if a $status variable is within a certain range (3xx for HTTP redirects). And in the end I wondered which one is faster: To calculate it or to compare that. Because the function I needed that in normally expects a value within the range, I decided to take the calculation because for these cases it’s faster. But both is pretty close.

* TEST #2: Speed comparison calc vs. logical expression 

#   INPUT          3==(int)($st/100)        300>$st||$st>399           RATIO
----------------------------------------------------------------------------
#1  298                     : 0.875473       / 0.824722              94%
#2  299                     : 0.872805       / 0.820043              93%
#3  300                     : 0.821013       / 0.917062             111%
#4  301                     : 0.866095       / 0.907742             104%
#5  307                     : 0.865981       / 0.906034             104%
#6  398                     : 0.865996       / 0.903764             104%
#7  399                     : 0.866033       / 0.955183             110%
#8  400                     : 0.820848       / 0.956306             116%
#9  401                     : 0.865922       / 0.956707             110%
(1 000 000 iterations each)

I somehow smell that something with modulo might be an option as well, but did not dig into that further.

Next: PHP: Compare vs. Calculating (2/2)
Read On: PHP: Casting vs. intval()

This entry was posted in Hacking The Core, Pressed and tagged , , , , , . Bookmark the permalink.

3 Responses to PHP: Compare vs. Calculating (1/2)

  1. if ( 3 == $st[0] ) wasn’t slow enough?😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s