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.


Category: programming Time: 2005-03-07 Views: 1

Related post

  • Benchmark for website speed optimization 2012-11-07

    I working on website speed optimization. I mostly used 3 tools for analyzing speed of optimization. Speed analyzing Tools: Google pagespeed tool Yslow Firefox extenstion Web Page Performance Test I am measuring performance using above tool and benchm

  • Nginx + php-fpm optimization review 2013-05-31

    Hello, I asked our hosting provider for nginx + php-fpm optimization on our managed server. I got this answer: Hi, There isn't much optimisation that can be done within Nginx as it is threaded and doesn't use many resources. I have checked that your

  • (bad) nginx + php-fpm optimization (done by our provider) review 2015-04-02

    I asked our hosting provider for nginx + php-fpm optimization on our managed server. I got this answer: Hi, There isn't much optimisation that can be done within Nginx as it is threaded and doesn't use many resources. I have checked that your php con

  • php artisan optimize times out only on composer install 2016-01-21

    I'm running a laravel app on Azure. Things are moving in the right direction, composer install didn't use to work at all. Now however, the "php artisan optimize" command times out: However, running "php artisan optimize" separately is

  • Apache Wordpress Speed Optimization - alternative php, memcached, wtc, cloudflare? 2016-01-24

    I currently have a LAMP stack on a digitalocean droplet (2gb, 2 core) I also setup Varnish It's a static site (blog) on frontend. I'm using: Varnish W3 total cache plugin Cloudflare cache Should I install alternative php or memcached or both? Should

  • PHP Wordpress optimization my loop code 2012-12-03

    I'm just learning PHP, and I'm using get_terms to get some text descriptions from the CMS, but i want to assign 3 variables to only 3 of my woocommerce tags. My code works well, but I just want to learn if there's a better way to filter by $tag_descr

  • Speed optimization of $wpdb- get_results 2012-12-17

    I'm using this code to generate all my custom field values from all posts for custom field named "autor". <?php $autor = $wpdb->get_results("SELECT meta_value AS autor FROM wp_posts, wp_postmeta WHERE post_status = 'publish' AND meta_

  • Speed optimization for girth-counting algorithm 2014-09-29

    I have the following girth-counting algorithm on a large 0-1 matrix and I need to optimize it for speed: function CountG8() M = 512 N = 1024 H = rand(0:1,M,N) girth_8 = 0 for i=1:M-3 for j=1:N-1 if H[i,j]==1 for j1=j+1:N if H[i,j1]==1 for i1=i+1:M if

  • Speed optimization for transparent gradient blend algorithm 2014-11-14

    The following algorithm works correctly for the purposes that I need, which is to set the top side of an image with a transparent gradient blend. Any tips to optimize it for speed? void ImageUtil::sectionAlphaGradient(uint32_t* pixelBuffer, const int

  • Speed optimization for block XOR 2015-01-09

    In code I'm currently maintaining, there is a need to do very many repeated XOR operations of blocks of memory. The block size in my case is always 16 bytes. Because the code is executed very frequently, and because it's speed critical, I would like

  • Speed optimization of shadow computation with grass gis r.sun 2015-09-15

    I am trying to optimize the speed of r.sun to compute shadowed area on a DTM at 1m resolution. My current area is 3.39 km² but my final area will be 450 km². The r.sun wiki say that pre-computing horizons as well as lat/lon increase the model, but th

  • PHP code optimization technique during echo statement 2016-01-19

    I am coding a php site from scratch. I am confused whether the following two codes are exactly same or are different with respect to server load and execution speed. Code 1: <ul> <?php $i =0; while($i < 10){ echo '<li>'.$row['database_da

  • PHP and optimization of MySQL query 2016-02-09

    I need to optimise SQL queries in my PHP code used by hugely over headed web service. I'am getting list of words. 1) get word identifiers 2) foreach identifier get word as php object 3) print word details in xml Now I have a code that takes this iden

  • Page Speed Optimization Questions: defer parsing and specifying a char set? 2011-12-09

    Continuing to optimized a web site, I'm using Chrome's Dev Tool add-on for Page Speed. I've been able to do a good bit of performance tuning thanks to its suggestions, but these two have me perplexed: I'm being told I need specify character set, but

  • Speed optimization on WP site, Remove query strings from static resources 2014-01-01

    This question already has an answer here: How to remove file versions from the file source links in wp_head? 2 answers I am using GTMetrix to test the speed of one of my sites. The report says that I should 'Remove query strings from static resources

  • Speed optimization of Mean calculation in a previous calculated data range 2014-11-10

    I have following problem: My calculations work quite well with the shown testdata, the only problem is the speed of it. My original dataset is about 64000 datapoints, and it takes several minutes to calculate the following script. I have the clue tha

  • Jquery: Speed optimization in jquery execution time 2016-01-27

    I am working with hierarchy data where i am using collapse and expand of childerns on click of parent node. Its working fine but time taking to excute is so much please help me to in optimization. View: <table id="data-grid-table-tree" class=

  • MySQL takes forever after running certain php scripts- optimization help? 2010-04-12

    When I run a couple scripts from the vBulletin software (like uninstalling a plugin) it takes forever. When monitoring the memory usage, it shows this => -/+ buffers/cache: 158 381 Swap: 255 10 245 It seems that MySQL is only using a certain amount a

  • Gtmetrix shows load time 90sec, but in reality it's 5 sec; how to optimize for speed? 2016-06-18

    I develop a WordPress website using Visual Composer Plugin. I have several issues. Website speed in getmetrix is 90sec, and 60sec in pingdom. But when I open website manually its speed is 5 sec.. What is the problem with: my website on gtmetrix or on

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.839 (s). 13 q(s)