a monochromatic oeuvre

» Testing Google’s PHP optimization tips

June 27, 2009

Testing Google’s PHP optimization tips

Recently, Google posted a series of articles titled “Let’s make the web faster“, including one by Google webmaster Eric Higgins about optimizing PHP.

Some of Google’s tips are obvious, like using caching or avoiding unnecessary SQL queries. A couple, however, seemed pretty weird to me, so I ran some benchmarks to check them out for myself. (See bottom of post for specs, and other info on how I ran the benchmarks)

  • Use echo instead of print: Higgins doesn’t explain why he thinks echo is faster than print. The PHP documentation links to an faq that explains the difference, and mentions that echo is marginally faster because it doesn’t set a return value, but there is no real difference in speed between the two.

    To test this, I ran two tests, using echo and then print to print a short string (5 characters) and then a long string (5000 characters) 1,000 times each.

    The Benchmark

    Using echo to print a short string 1,000 times: 1170 microseconds
    Using print to print a short string 1,000 times: 1180 microseconds

    Using echo to print a long string 1,000 times: 1163 microseconds
    Using print to print a long string 1,000 times: 1182 microseconds

    Verdict: For short strings, echo was slightly faster, and for long strings, it looks like print was slightly faster. For both cases, this was a difference of only about a couple hundred-millionths of a second per call, and likely well within normal variation between calls. I’d say use whichever one you feel like using, personally.

  • Use switch/case instead of if/else for loosely-typed comparisons:Apparently this has better performance, readability, and maintainability. I’ll bit on the readability and maintainability, but this article is about performance.

    I’m testing with code very similar to the example that Higgins provided for this one. There are four branches, each of which calls a function that does nothing but return, and each branch is equally likely.

    The Benchmark

    Using if/else, repeated 1,000 times: 422 microseconds
    Using switch/case, repeated 1,000 times: 425 microseconds

    Verdict: Once again, the difference is barely noticeable, and it looks like if/else was actually slightly faster on this test. I would say not to worry too much about it, though the switch/case definitely looks better from a code readability point of view.

  • Provide echo with many strings as arguments instead of using concatenation: In the video, Eric replaced each of the periods in an echo call with commas, so that the strings were passed as many arguments to echo instead of concatenating them before passing them in. This seems definitely weird to me, but it might work.

    For this one, I’m testing echo "Hello "."World!"."\n"; vs. echo "Hello ","World!","\n";

    The Benchmark

    Using concatenation, repeated 1,000 times: 1225 microseconds
    Using arguments, repeated 1,000 times: 3255 microseconds

    Ouch! No way is it faster to pass in multiple arguments to echo than concatenation them first! I notice that I have three arguments to echo and that took almost 3 times as long to run… let’s try it with 6 arguments. This time I’m testing echo "Hello "."World!"."\n"."Hello "."World!"."\n"; vs. echo "Hello ","World!","\n"." ","World!","\n";

    The Benchmark (2)

    Using concatenation, repeated 1,000 times: 1445 microseconds
    Using arguments, repeated 1,000 times: 5354 microseconds

    It’s not quite linear, but it’s definitely correlated.

    Verdict: There’s no question about it. Use concatenation! It’s better! Google doesn’t know everything!

The moral of this story… Google is smart, but they don’t know everything. Test things for yourself to make sure their results are the same as yours. It’s possible that Google is using a different version of PHP, running on different hardware, or something like that. Each computer is different and if these small optimizations matter to you, you have to try it for yourself to make sure.

Edit: The PHP devs and at least one other blogger have reached about the same conclusions.

Note: I ran these benchmarks on my Linode VPS, with 360MB RAM and 4×2.50GHz processors. I was using PHP 5.2.6-3ubuntu4.1 with Suhosin-Patch 0.9.6.2 from the command line, and the PHP Benchmark library. Each reported time was an average over 100 trials. Source code for my benchmarks is available here. You have to pipe stdout to /dev/null, since I’m using print and echo. Results are in stderr.

§

Comments

RSS feed for comments on this post. TrackBack URL

Leave a comment

§

Resume - Sandbox - Music

© 2008-10 Marquis Wang