In some situations using a double quotes string is required i.e. “this\nstring\nappears\over\nmany\nlines” …
However in 99% of cases it is used without even thing about in implications of doing so … PHP will infact evaluate any string wrapped in double quotes, this adds a processing overhead, but it seems people do not actually reliase how much in comparrison to using single quotes for the same string.
Take for example this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?PHP /** * double-quotes-are-bad.php ~ D.Busby (Saiweb.co.uk) **/ $start = microtime(true); $var = "This is a stiring it may not actually have anything to be parse within" . " However the issue remains that infact php will attempt to evaluate every char" . " In this string, which in this example may not be so bad, as it's just one string" . " In one file, buit imagine what happens when every string in your webapp uses double quotes"; $end = microtime(true); $len = strlen($var); $res = round($end-$start,10); echo $len.' Chars evaluated in '.$res.' seconds'."\n"; $start = microtime(true); $var = 'This is a stiring it may not actually have anything to be parse within' . ' However the issue remains that infact php will attempt to evaluate every char' . ' In this string, which in this example may not be so bad, as it\'s just one string' . ' In one file, buit imagine what happens when every string in your webapp uses double quotes'; $end = microtime(true); $len = strlen($var); $res2 = round($end-$start,10); echo $len.' Chars evaluated in '.$res2.' seconds'."\n"; $speed = round((1 - $res2/$res) * 100,2); echo 'Single quotes are '.$speed.'% faster'."\n"; ?> |
Now I am running this on a live server, that is serving in excess of 100 pages a second, take a look at the output:
320 Chars evaluated in 1.40667E-5 seconds
320 Chars evaluated in 3.0994E-6 seconds
Single quotes are 77.97% faster
320 Chars evaluated in 1.28746E-5 seconds
320 Chars evaluated in 3.0994E-6 seconds
Single quotes are 75.93% faster
320 Chars evaluated in 1.3113E-5 seconds
320 Chars evaluated in 2.1458E-6 seconds
Single quotes are 83.64% faster
320 Chars evaluated in 1.19209E-5 seconds
320 Chars evaluated in 2.861E-6 seconds
Single quotes are 76% faster
320 Chars evaluated in 1.3113E-5 seconds
320 Chars evaluated in 2.861E-6 seconds
Single quotes are 78.18% faster
320 Chars evaluated in 1.3113E-5 seconds
320 Chars evaluated in 2.861E-6 seconds
Single quotes are 78.18% faster
The improvement is consistently in excess of 75%, so the moral of the story? don’t use “” if you do not need to!
Thanks to everyone along the way who’ve discussed and proven development methods along the way with me, and sorry it’s taken so long to get them written up.
Entries (RSS)
Sadly Buzz your PHP is still in need of some ‘help’
However you are a man after my own heart. Single quotes are indeed wonderful and actually now I have been using them solely for a couple of years they are the norm for me.
You were incorrect with your “this\nstring\nappears\over\nmany\nlines” comment. The correct single quote method would be…
‘this’ . “\n” . ’string’ . “\n” . ‘appears’ . “\n” . ‘over’ . “\n” . ‘many’ . “\n” . ‘lines’
Or perhaps:
$array = array(‘this’, ’string’, ‘appears’, ‘over’, ‘many’, ‘lines’);
$string = implode(“\n”, $array);
Although the second one is more elegant I doubt it’s really any good for static text. Feel free to run speed tests though.
cheers
Sean
ps: now a dad!
You kinda missed the point see i.e (in Example) … as in the double quoted string was an example, as in NOT optimized …
Anywho congrats on the new edition
Hi Buzz,
The reason double quotes is slower, is that the string is parsed. Everything is single quotes is taken literally and thus not parsed. Thus you should not compare the speed, but the functionality of the two. Speed should only be an issue if it really it negatively effects performance. Using double quotes can be very beneficial in that you do not have to concatenate strings together with variables and escapes.
Unless you are working with very huge strings, the performance difference is negligible. Many other factors become important way before that. A good approach is to understand bottlenecks (such as disk), and performance of code. see: http://en.wikipedia.org/wiki/Computational_complexity_theory
Here is a good article on micro-optimization (which is what you’re doing when using single quotes instead of double quotes). Though published in 2006, it still applies today.
http://www.acm.org/ubiquity/views/v7i24_fallacy.html
@bucabay
I did say “don’t use “” if you do not need to!” as with all things this needs to be your own best judgment, there are of course caveats such as sacrificing functionality for performance, which in some cases as you say may be a minor gain.
Thanks for the reference material though always appreciated