on Mythbusters

PHP Myth Busters: Using single quotes on string is faster than double quotes

14 comments
code
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

I got idea today talking to my co-worker and friend Zoran about starting a series of article called: PHP Myth Busters. This series will deal with some PHP Myths and try to Confirm them or Bust them. Please, feel free to contribute by sending some myths around PHP which you would like to put to the ultimate test.

I will start with a simple Myth today:

Using single quotes on a string in echo statement is faster then using double quotes!

When I started the research on this, I was wondering how to even prove (or disapprove) such a statement. We can not just measure the time in ms and that’s it.

My research of the internet has lead me to PECL extension called Vulcan Logic Disassembler :-) or VLD, which can be used to see the generated PHP Opcode (the compiled form of a PHP script).

So I installed the extension and started testing.

First, double quotes:

echo "This is a string";

Opcode looks like this:

ECHO       'This is a string'

Then, I tried single quotes:

echo 'This is a string';

Opcode looks like this:

ECHO       'This is a string'

Same result.

MYTH IS BUSTED!

But wait. As I wanted to call this myth busted, I played a little with adding some variables in the game:

echo "This is a $variable";

I got this opcode:

INIT STRING  ~0
ADD_STRING   ~0 ~0 'This'
ADD_STRING   ~0 ~0 ' '
ADD_STRING   ~0 ~0 'is'
ADD_STRING   ~0 ~0 ' '
ADD_STRING   ~0 ~0 'a'
ADD_STRING   ~0 ~0 ' '
ADD_VAR       ~0 ~0 !0
ECHO            ~0

OMG. Very much opcode generated out of one simple echo statement. Let us try to optimize that somehow:

echo "This is a " . $variable;

I am using concatenation this time, and it is much simpler opcode now:

CONCAT       ~0 'This is a ' !0
ECHO           ~0

Wow, that is obviously much faster then before. Even PHP manual agrees:

Parsing variables within strings uses more memory than string concatenation. When writing a PHP script in which memory usage is a concern, consider using the concatenation operator (.) rather than variable parsing.

Some people on the net suggest that the fastest form is this:

echo "This is a " , $variable;

Yes, a comma. You can use it in echo statement. This produces :

ECHO       'This is a ' 
ECHO       !0

This is the fastest way, as it is not duplicating the string like in Concatenation.

I know, you will tell me that it is not worth it, but it isn’t much of a change to use commas to concatenate parameters in echo statements. If you have many, it could be a great optimization.

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon



  • Pingback: Tweets that mention PHP Myth Busters: Using single quotes on string is faster then double quotes | CodeForest -- Topsy.com

  • vijay

    great article love learning little tips and tricks to make my app work that much faster

  • Mike

    Single quotes and concatenation it is! :)

  • http://twitter.com/nine_L Lenin

    Well are you not also pleading for micro-optimizations? http://www.phpbench.com/

  • http://milki.erphesfurt.de/ mario

    Well actually, 13.8%

    There is no performance difference at runtime. But it might affect parsing time. More exactly the PHP tokenizer needs a few more cycles to look for placeholders in double quoted strings. You cannot measure the 13% in practice, that’s nuts. You only see it when you have PHP parse/tokenize a few billion strings through eval().

    Again. There is no real performance gain using single quotes. It’s measureable but not significant.
    http://milki.erphesfurt.de/EPCCP.pdf # page 9

  • Ned

    Good start.

    I like the idea of PHP myth busting.

  • Monkeytail

    I use single quotes.. not because of ‘the speed’ just because it types a milisecond faster (double quotes requires the shift key)

    • http://www.steelesoftconsulting.com/ John

      Here, here! My pinkys are ‘closer’ to the Ctrl keys than the Shift keys.

      Not to mention is takes up less ‘space’ in your editor.

      Where is the the test for:

      echo ‘This is a ‘. $variable;

      Never liked the comma, looks more like a function argument.

      Interesting article, should have thrown print in there though.

  • Gavin Fonseca`

    Interesting Article. Correct me if I am wrong, but, looking at your code and the opcode, it’s seems to me that the compiler is converting the double quotes to single quotes. So the most optimum way would be to use single quoted strings and ‘,’ when ECHOing and single quoted strings and concatenation when saving to a variable (eg. when building a query)

    • http://www.steelesoftconsulting.com/ John

      And who wants to go and change the commas in the echo when you decide you want to manipulate it and turn it into a variable?

      Would have been nice to see a test like this to go along with the article:

      $var = ‘This is a variable’;
      $out = <<< EOO
      In PHP, $var.
      EOO;
      echo $out;

  • http://www.zhaokunyao.com huarong

    use less memory not means run faster~~~

  • Ümit

    Hi,
    Sample code which compiled version of php?

  • Roman

    Looke here – http://www.phpbench.com/
    Section: String Output

    echo ‘aaaaaaa’,’aaaaaaa’,’aaaaaaa’,’aaaaaaa’ is slow

  • Pingback: Anonymous or lambda functions in PHP