php goodness

Some PHP performance myths

Posted in Short by phpgoodness on July 23, 2010

Let’s do some benchmarking and prove (or disprove) some urban legends.

Echo vs Print

Echo claimed to be quicker, since actually it’s not a function (but a language construct), and therefore it doesn’t return a value. Print is not a real function either, but acts like a function, and always returns 1.

Also, to echo you can pass comma-separated strings and it will output hem right aways, one by one (just like your would have several different echos following each other), that is, PHP doesn’t need to concatenate them first in the memory, they can be written to the output immediately.

Average, relative 1000 10000 1000000
echo, with comma 1,00 700ms 6528ms 583318ms
echo, with concat 1,06 740ms 6425ms 670096ms
print, with concat 1,12 773ms 6841ms 700804ms

As you can see the theory is works in practice, although the difference is not that big. I used 4 separate strings with 4-8 characters. If you code (and if you still use direct output writing at all), try using echo with commas.


Incrementing (or decrementing) an integer with 1 is a quite common operation again. You can do it (at least) in 4 ways using PHP.

Average, relative 1000 10000 1000000
++$j; 1,00 281ms 2375ms 228295ms
$j++; 1,20 358ms 2759ms 265173ms
$j += 1; 1,20 342ms 2843ms 273779ms
$j = $j + 1; 1,33 368ms 3150ms 307458ms

++$j (pre-increment) is markedly quicker than the others (20%-30%). Why? All the other operations first copy the value of $j to the memory, increment it there (in that temporary register) and then assign it to $j. This is obvious for the last 2 cases, but what about $j++ (post-increment)?

The difference between the two increment operator is that the latter ($j++) first return the original value of $j, and then increments it. That is, it needs the copy of the value. If use increment operation in a void context (that is, you simply want to increment the value), use ++$j.


I think it is a not very well-known performance issue. Apparently PHP uses string comparison to determine is an object is an instance of a class. Though instanceof operator is pretty quick, it’s not all the same, how long is your class name.

Average, relative 1000 10000 1000000
Short classname: (1 char): 1,00 449ms 3615ms 356093ms
Long classname (26 char): 1,84 760ms 6852ms 684316ms
Very long classname (78 char): 3,74 1495ms 14234ms 1405590ms

See? Pretty amazing difference. So readability vs performance? Since instanceof is not a frequently used operation, I vote for long (but readable) class names, but if you use if often, you may consider removing some extra characters.

For the tests I used PHP 5.2.5, command line execution on Debian. On a request I can add the benchmarking scripts to the post.


Leave a Reply

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

You are commenting using your 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

%d bloggers like this: