Programming - - By Thomas Rutter

PHP Speed Optimizations

Nathan Wong claims that single quotes are faster than double quotes in PHP.

In the recent blog post, he attempts to prove that PHP is able to parse and execute string constants faster if they use single quotes, than if double quotes are used. Benchmark results and source code are provided.

The reasoning behind this speedup is that when the PHP compiler encounters a string constant beginning with a double quote, the string itself must be parsed and special characters (such as $, and {) have special meanings. By contrast, if a string constant begins with a single quote, only the backslash has a special meaning, and therefore PHP doesn’t have to worry about replacing variables and expressions embedded in the string.

Nathan’s results indicate an approximate 16% speedup when using single quotes in his testing.

This isn’t the first time this optimization technique has surfaced. This PHPLens article by John Lim is a very comprehensive guide to small optimizations in PHP, and has been updated over time to reflect changes in PHP. The article explains that while some optimizations will improve the speed of a very small application, the same optimizations may have a negative effect when scaled to larger applications. John uses the analogy of the sprinter and the marathon runner when describing optimizations that work well for speed and those that work well for scalability.

Interestingly, however, John places the ‘single quotes vs double quotes’ issue into an appendix named ‘Useless Optimizations’ (near the bottom).

‘var=’.$var is faster than “var=$var”
This used to be true in PHP 4.2 and earlier. This was fixed in PHP 4.3. Note (22 June 2004): apparently the 4.3 fix reduced the overhead, but not completely. However I find the performance difference to be negligible.

Thus, while using single quotes instead of double quotes may have a slight performance improvement, its effect is negligible, and he rightly points out that there are many other optimizations that will make a bigger difference to the speed and scalability of your application.

I’d recommend you read the article as it’s a great read and well-written.

When optimizing any code, one should focus only on those areas of the application which take up a significant amount of its running time. Optimizing a section of a code that is only executed once will not be worth your time as much as a section of code that is executed many times. So, if you have a loop whose contents will be executed many times, it would be worth optimizing that which occurs inside the loop.

Another point to make in code optimization is that you will need to compare the cost (in your time, or a programmer’s time) of doing the optimization with the cost of upgrading server hardware. Hardware is comparatively cheap.

In my searching around for actual benchmark data, I found this page of PHP Benchmark Tests. Interestingly, the statistics shown on this page are live – the raw figures shown are computed by the server each time you load the page.

Many of the tests done last only for a few milliseconds in total, so the accuracy of the benchmarks may not be reliable. However, even these help to illustrate which optimizations aren’t very important. The most telling statistics are those for various methods of looping through an array, with the slowest method taking eight times as long as the fastest.