PHP: Compare vs. Calculating (2/2)

A comment by Thomas Scholz in my (that time one-parted) PHP: Compare vs. Calculating post made me to take a reprise on the comparison.

He suggested to just compare to the first character to find out if a status number is between 300 and 399. This is possible because in PHP a variable’s type depends on the context in which it is used – no strong typing in PHP by default. This can be problematic if the number is not between 100 and 999 (for my scenario). Since these tests are more or less to find out more about the language, I left this minor problem aside and did more tests to find out about the performance of this method.

While doing so, I added two additional tests to compare against the exact type as well, because I did not in the first place (using the identical comparison operator ===; see Comparison Operators and Type Juggling). So this has another update on the first test with a proper value/type comparison as well and two additional tests with Thomas’s suggestion. Next to that I needed to run all tests again in one batch because I changed the testing environment so to make all four runs comparable. The results:

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

#   INPUT          3==(int)($st/100)       300>$st||$st>399           RATIO
----------------------------------------------------------------------------
#1  298                     : 1.006099       / 0.912567              90%
#2  299                     : 1.006275       / 0.914001              90%
#3  300                     : 0.907436       / 1.034375             113%
#4  301                     : 0.999220       / 1.033318             103%
#5  307                     : 0.999821       / 1.031399             103%
#6  398                     : 0.998455       / 1.029523             103%
#7  399                     : 0.997352       / 1.030377             103%
#8  400                     : 0.902444       / 1.029886             114%
#9  401                     : 0.997587       / 1.030117             103%

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

#   INPUT          3===(int)($st/100)       300>$st||$st>399           RATIO
----------------------------------------------------------------------------
#1  298                     : 0.982984       / 0.916047              93%
#2  299                     : 0.982830       / 0.913736              92%
#3  300                     : 0.878410       / 1.038408             118%
#4  301                     : 0.982108       / 1.035703             105%
#5  307                     : 0.981841       / 1.032421             105%
#6  398                     : 0.982735       / 1.031907             105%
#7  399                     : 0.983007       / 1.032652             105%
#8  400                     : 0.878910       / 1.031816             117%
#9  401                     : 0.981827       / 1.032441             105%

* TEST #3.a: Speed comparison calc vs. string

#   INPUT          3==(int)($st/100)         3 == $st[0]             RATIO
----------------------------------------------------------------------------
#1  298                     : 1.011356       / 0.957462              94%
#2  299                     : 1.008264       / 0.955943              94%
#3  300                     : 0.904260       / 0.958110             105%
#4  301                     : 1.004242       / 0.958032              95%
#5  307                     : 0.998767       / 0.956585              95%
#6  398                     : 0.999254       / 0.958502              95%
#7  399                     : 0.999853       / 0.956734              95%
#8  400                     : 0.903021       / 0.957968             106%
#9  401                     : 0.999693       / 0.958988              95%

* TEST #3.b: Speed comparison calc vs. string

#   INPUT          3===(int)($st/100)        3 == $st[0]             RATIO
----------------------------------------------------------------------------
#1  298                     : 0.978594       / 0.948607              96%
#2  299                     : 0.980500       / 0.949775              96%
#3  300                     : 0.874215       / 0.949211             108%
#4  301                     : 0.977630       / 0.950864              97%
#5  307                     : 0.978283       / 0.950362              97%
#6  398                     : 0.978113       / 0.948841              97%
#7  399                     : 0.978710       / 0.949246              96%
#8  400                     : 0.874446       / 0.949209             108%
#9  401                     : 0.978745       / 0.948915              96%
(1 000 000 iterations each)

If you wonder where test #1 is, it’s of no use for this comparison, so I skipped it.

Next to what already could have been seen in part 1 (test #2.a), using string access is fast. It most often is even faster than doing an integer division calculation. The integer calculation is only faster with a number like 300 or 400 in the division by 100 operation. This spares PHP some time to switch to float (which it does normally for division ) . But in this case PHP does not need to switch over to floating point calculation for these two cases (300, 400) only, so it’s slower and and accessing character offsets of a string is faster. This makes sense.

The other thing that the new tests show is, that using the identical operator in comparisons is not hurting as well. Instead it saves PHP some cycles to run to not fall into type-juggling and it saves some variable context trade-offs. It’s already posted here and there (stumbled over a nice post about this lately but I do not remember the URL), that you should use that operator in PHP more often – so to say ever then it fits and that is mostly the case. This is something every PHP code can actually benefit from.

Previous: PHP: Compare vs. Calculating (1/2)

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

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