views:

187

answers:

5

A web site, 5 human years in code (5 developers, approx one year), 10 of thousands of hits every day. Is it really going to have an impact if we change all " to ' where possible?

+8  A: 

No.

As somebody pointed out in a PHP micro-optimization question a few weeks back (I think the issue was the same ' versus "), implementing the optimization will almost always cost more time than would ever be saved by it.

Pekka
+9  A: 

See the double (") vs. single (') quotes section at PHPBench (scroll to the end of the page). When I invoked the page, I saw the following results:

Single Quotes: 257 µs
Double Quotes: 232 µs

Unless 25µs makes a difference for your problem domain, it doesn't matter.

I'm going to try to work out the math here, someone correct me if I make a mistake. Let's say that you have 10,000 places where you used double quotes instead of single quotes at a 25µs difference per page load (who knows if it actually will work out that way, you need to benchmark your actual code) that would be a would be a 0.25 second difference in execution time. That could be significant depending on server-load if you were Facebook. However, I suspect that there are many other places in your codebase that are far more resource intensive that you will want to optimize before you even look at quotes. Just look at the pain points highlighed on the PhpBench page and you will see that you should have different priorities. Also, these numbers will be drastically different if you run a different php implementation like Quercus - which can solve some performance bottlenecks.

Elijah
What're "quites"? J/K +1 :D
Mark
But it's a good idea to use single quotes for any new PHP projects, right?
Baddie
If you were Facebook you'd be using faster servers. ;)
musicfreak
If these tests show anything then it's "short synthetic tests usually suck" or in a friendlier version "they can only tell you that it doesn't matter" ;-) I ran test 8_1 and 8_2 on my desktop and the first result was: single is faster than double quotes. Running the tests 10k times revealed that the variance is much bigger then the alleged "advantage" of double quotes (257 vs 232, that's almost 10%). Other,unaccounted factors obviously have a much bigger impact. (And simplified microtime() benchmarks measuring such short periods and having such tiny results most often tell you ...nothing)
VolkerK
+6  A: 

Yes, you'll end up making that one mistake where you change "$test\n" to '$test\n'.

Real-numbers-wise, no.

Derek Illchuk
Could you please explain how those two examples differ.
mjdth
The first prints the value of your `$test` variable followed by a newline. The second prints, literally, `$test\n`.
Derek Illchuk
More verbosely, double quoted strings evaluate variables and escape sequences and single quoted strings take the straight text literally with no substitution. So by changing double quotes to single quotes in the example above, you've introduced a bug in the code.
Asaph
+2  A: 

The risk of spending time on a micro-optimiztion attempt is that there are probably (yet to be identified) aspects of the code (and any third-party dependencies) which could/should be profiled, then refactored/optimized with a much better efficiency-increase per man-hours-spent ratio.

I can't recommend enough that you read Remember also the rules of Optimization Club.

micahwittman
Think of it not as micro optimization but as guidelines I need to setup for the programmers before they start this huge project...
Itay Moav
That's a fair point.
micahwittman
A: 

There is no difference in peformance at run time. But there is a difference at compile time.
The usual way of thinking about quotes in PHP is:

Variables inside single-quoted strings are not parsed, and so the PHP engine doesn't have to waste time looking for them. Hence, single-quoted strings must be more efficient

That assumption is false because the PHP engine doesn't perform that ckeck at run time, but at compile time.
When PHP is parsing your script, if it finds a double-quoted string that has no variables inside it, then that string is treated as a constant string, i.e. its value won't change in the entire script's life-time (if there are variables inside, then it's converted to string concatenations).
Likewise, if PHP finds a single-quoted string, it is treated as a constant as well. But in this case PHP doesn't have to parse the content of the string lookng for variables.

So the answer to your questions depends on whether you are using a PHP caching solution or not (APC, eAcceleator, etc...). If the compilations of your scripts are not being cached then you might have a slight performance improvement because your scripts are compiled every time a visitor loads a page, and hence compilation time is important. But if the compilations of your scripts are cached, then there is no different on using single- vs double-quoted strings.

GetFree