2

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?

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • 5
    It's always interesting to see people trying to micro-optimize PHP code given that PHP as a platform routinely benchmarks almost an order of magnitude slower than Java and .Net. If you're using PHP, you're already hopelessly behind the 8-ball. PHP has a lot of strengths but performance is not one of them. If this kind of micro-optimization is critical to your app, you've chosen the wrong language. – Asaph Dec 24 '09 at 03:33

5 Answers5

9

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
  • 13,368
  • 10
  • 57
  • 89
  • 1
    But it's a good idea to use single quotes for any new PHP projects, right? – Omar Dec 24 '09 at 02:53
  • If you were Facebook you'd be using faster servers. ;) – Sasha Chedygov Dec 24 '09 at 02:57
  • 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 Dec 24 '09 at 08:46
8

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
  • 442,112
  • 142
  • 972
  • 1,088
6

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

Real-numbers-wise, no.

Derek Illchuk
  • 5,638
  • 1
  • 29
  • 29
  • Could you please explain how those two examples differ. – mjdth Dec 24 '09 at 02:46
  • 3
    The first prints the value of your `$test` variable followed by a newline. The second prints, literally, `$test\n`. – Derek Illchuk Dec 24 '09 at 02:49
  • 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 Dec 24 '09 at 03:37
2

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.

Community
  • 1
  • 1
micahwittman
  • 12,356
  • 2
  • 32
  • 37
0

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
  • 40,278
  • 18
  • 77
  • 104