Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« PEAR Channels
Knocked off the internet, a story of Windows and Macintosh »

Microbenchmarks of single and double qouting.

March 8th, 2005

I wrote earlier about flawed microbenchmarks. Today on sitepoint, there was a post on the performance of single versus double quotes in PHP based on a microbenchmark:

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.

This line of thought is flawed for two reasons.

First, PHP uses flex to tokenize the php source code. Flex is a table driven lexer. It doesn’t necessarily follow that having more characters with special meaning makes flex slower if you don’t actually use those characters.

Second, the original benchmark doesn’t actually measure parsing time. Like all microtime() based benchmarks, the first access of microtime() doesn’t happen until the php file is completely parsed and converted into opcodes. Additionally, it executed each literal inside a 10,000 iteration loop. Thats 1 time to parse it and 10,000 times to execute it.

I put together a different benchmark that does measure parsing time. It consists of two files

 
<?php echo 'very long string'; ?>
 

and
 
<?php echo "very long string"; ?>
 

The very long string I used was the text of the US constitution: 27,787 bytes. I measured using ab, which includes parsing time. My results:

single quotes: 27.59 ms
double quotes: 27.74 ms

Hardly earth-shattering. Single quotes was consistently faster, but by the same minor margin. This is with a 27k literal string. My guess is that you will never have enough literals in a typical PHP program for any parsing performance differential to matter.

Is there an execution differential? Do both of these constructs generate the same opcodes? I tried to check it out using Vld or parsekit but I can’t get either of these to work in my development environment (OS X). Perhaps someone will reply in a comment with the opcodes for the previous two programs and settle the question.

There doesn’t seem to be much if any difference between single and double quotes for an identical string. A more interesting question would be what happens when the strings aren’t identical. Consider the following three examples:

 
<?php echo "Answer: $answer!"; ?>
 

versus
 
<?php echo 'Answer: ', $answer, '!'; ?>
 

or
 
<?php echo 'Answer: ' . $answer . '!'; ?>
 

or
 
Answer: <?php echo $answer; ?>!
 

Which one do you think is faster?

My gut feeling is that concatenation is slower because of the ability of the output buffer to allocate memory in big chunks rather than little chunks. Other than that, I honestly don’t think it makes a difference, but I would love to compare the opcodes for each of these examples.

categories PHP
tags benchmarks, performance-optimization, PHP

Related Posts

  • Flawed Microbenchmarks
  • The Usability of Input Filtering
  • Benchmarking PHP’s Magic Methods
  • Two preg_replace Escaping Gotchas
  • Crazy Zend PHP Framework Rumors
You can leave a response, or trackback from your own site.

11 Responses to “Microbenchmarks of single and double qouting.”

  1. #1 ilia alshanetsky responds...
    March 8th, 2005 at 6:00 pm

    The opcodes generated for both scripts will be the same:

    line # op fetch ext operands
    ——————————————————————————-
    1 0 ECHO ‘very+long+string’
    1 RETURN 1

    The very minor difference is during the parsing stage, it would be eliminated entirely had a compiler/opcode cache been used.

    As far as other examples go,

    generates 7 opcodes vs

    and
    that generate 4 a pieces.

    This once again proves that concatentation wins over variable “inlining”.

    Answer: !

    Is 4 opcodes long as well, however they are most echo opcodes vs the prior two perform internal concacts. Therefor speed wise I suspect the last version of the code would be faster.

  2. #2 ilia alshanetsky responds...
    March 8th, 2005 at 6:02 pm

    Oops slight typo, the actual number of opcodes is 8 (for inlined var) and 5 for the rest.

  3. #3 Pure-PHP responds...
    March 9th, 2005 at 12:29 am

    Answer: ! is faster, because there is no concatentation

  4. #4 Hendy Irawan responds...
    March 9th, 2005 at 6:04 am

    Whew!

    I never knew people worried about this teeny little teetsy bit of performance so much!! Phew!

  5. #5 Pure-PHP responds...
    March 9th, 2005 at 10:53 am

    Maybe you shuold use htmlentities, because php tags are not displayed in comment. For example in my last comment
    Answer: < ?php echo $answer; ?>!

  6. :: onpk :: blog PHP, MySQL, développement web & connexe(s) trackbacked on March 10th, 2005 at 9:06 am
  7. Jeff Moore’s Blog » Blog Archive » The Usability of Input Filtering pingbacked on March 31st, 2005 at 11:24 pm
  8. templaterie blog - Quotes in PHP pingbacked on May 29th, 2005 at 3:54 pm
  9. #9 Scott Mattocks responds...
    April 14th, 2006 at 5:56 am

    In your test, you used one gigantic string. This is not very realistic. It is much more likely that a script will contain a whole lot of small strings. For example, a configuration file could easily have 2000 strings with just one or two words. Can you give me an idea of the difference between single and double quotes for lots of little strings.

  10. #10 Dan responds...
    July 3rd, 2006 at 10:06 pm

    http://us2.php.net/echo#66391

  11. #11 Dan responds...
    July 3rd, 2006 at 10:18 pm

    I guess that one uses microtime(). Of course the numbers will be different on different setups, but the relative results are what is important. I don’t see where this person’s test could be considered useless. It seems perfectly valid and well-constructed to me. I wish I could subscribe to these comments, because this is a topic in which I am interested. If anyone could drop me an e-mail regarding any important developments, I’d really really appreciate it. :-)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

  • Search

  • Subscribe

    Subscribe All Posts
    Subscribe All Comments
    Subscribe All Bookmarks
    Subscribe with Bloglines Subscribe with My Yahoo Add to netvibes Subscribe in NewsGator Online Add to Google
  • Share This

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (16)
    • Open Source (14)
    • PHP (95)
    • Software Design (28)
    • Usability (14)
    • WACT (7)
    • Web Design (20)
  • Recent Comments

    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  117
      Daniel Wabyick, Ya, Paul [...]
    • PHP Development From Java Architects Eye  12
      peter, qmenqyqh, big [...]
    • Why is PHP Popular?  34
      portfelmake.ru, finance-x.ru, economnedv.ru [...]
    • The PHP scalability saga continues  35
      mvyqfarib, fuck, sorwostajufe [...]
    • Delphi for PHP  29
      Shawn White, Chris MacPherson, Pieter Viljoen [...]
    • How to Transfer Mac OS X Application Data between Computers  41
      ??????????, Edward Gan, Kamut Torok [...]
    • php | tek Wrapup  7
      Sergey Brin, Learn Programming, PHP Guru [...]
    • A WordPress bug fix  5
      Juggigomugs, LkM, wordpress quickstart guy [...]
    • The value of MVC  11
      wan optimization, Ron, Caren Goodman [...]
    • php | tek 2008  3
      CharnaD, NatureLimit, Scott
    • Why is PHP Code Considered Hard to Maintain?  26
      Tom, , Visitor338 [...]
  • Pages

    • Tags
  • Recent Posts

    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
    • Keywords and Language Simplicity
    • Improved Error Messages in PHP 5
    • Michigan Taxes Graphic Design Services
    • Ruby versus PHP or There and Back Again
  • Archives

    • 2008: May
    • 2007: Jan Feb Mar Apr May Sep Oct Nov
    • 2006: Jan Feb Mar Apr May Jun Jul Oct Nov Dec
    • 2005: Jan Feb Mar Apr May Sep Oct Nov Dec
    • 2004: Apr May Jun Jul Aug Sep Oct Nov
  • Menu

    • Register
    • Log in