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.
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.
Oops slight typo, the actual number of opcodes is 8 (for inlined var) and 5 for the rest.
Answer: ! is faster, because there is no concatentation
Whew!
I never knew people worried about this teeny little teetsy bit of performance so much!! Phew!
Maybe you shuold use htmlentities, because php tags are not displayed in comment. For example in my last comment
Answer: < ?php echo $answer; ?>!
Les outils de RAD et le monde PHP
Le 14 avril prochain aura lieu le prochain Rendez-Vous AFUP : DreamWeaver, un outil de RAD pour PHP. Les inscriptions sont ouvertes depuis 2 jours seulement et au rythme où vont les choses, ça sera très vite plein (on peut accueillir 50 personnes et je…
[...] iltering a new examination. My problem with filtering is with usability. The comments to this post are a good example. There are o [...]
[...] Forum die Antwort, die Performance sei unterschiedlich. Singel ist schneller. Stimmt wohl: die mit singel quotes wird die amerikanische Verfassung [...]
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.
http://us2.php.net/echo#66391
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.
$a = “Hello “;
$b = “World”;
(a) $c = $a.$b;
(b) $c = “$a$b”;
I would like to know which one is faster to concat (a) or (b). OR if there is another faster way to contact, let me know.
so you did a test that has no real practical application, and then DIDN’T do the test that’s actually a typical use case scenario?
pointless.
This is a really desirable get learning on behalf of me. must admit that you are one of theo- coolest bloggers i always saw. thanks because of posting that useful article.
Hello! Fine post! Please when all could see a follow up!
hey there and thank you for your info – I have definitely picked up something new from right here. I did however expertise several technical points using this website, since I experienced to reload the site lots of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I’m complaining, but slow loading instances times will very frequently affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I am adding this RSS to my email and can look out for much more of your respective fascinating content. Make sure you update this again soon..
Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is fantastic blog. A great read. I will certainly be back.
Great write!.. I’ve been looking around for this for a while now. Thank you Bing for bringing me to your page lol. I can usually rely on Google to get the very best data. Anyways, I discovered some related information about Awesome Cloverdale accounts payable agency also. You may possibly want to check them out (http://cloverdalecollectionagency.info). Fairly intriguing read, also. Anyways, thank you for your post! If you have anything else related, please shoot me and email to let me know. Take care! BTW .. I bookmarked you on Digg as Microbenchmarks of single and double qouting. – Professional PHP
Great beat ! I wish to apprentice while you amend your site, how can i subscribe for a blog web site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear concept
If you dont know precisely what you want or need, you should think over the following conclusions.
Toller Schnäppchen. Sicher nicht verkehrt, gegenseitig inklusive einer Thematik intensiver auseinander zusetzen. Ich konnte bestimmt jene nächsten Beitraege in dem fach Auge behalten.
Lovely sharp post. Never considered that it was that effortless. Praises to you!
Get plenty through your notions,and i favorite all of your post!
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your website with more information? It is extremely useful for me. Regards, Nella.