As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Note that echo 'these are ', $foo; is faster than any of those, since there is no concatenation or interpolation.
–
PaulproJan 4 '12 at 22:25

1

Why on Earth is this question not constructive?
–
Chris HarrisonJul 23 '14 at 11:05

1

No idea. It was really a seed question, added shortly after the start of the site, when the beta people were encouraged to post baseline questions that would come up in early google searches, even if they were far too simple of a question, or bordering on non-question form. Given the views and activity of comments and voting therein, I'd say it was pretty constructive, imho.
–
UberfuzzyJul 23 '14 at 14:57

So, as expected, the interpolation are virtually identical (noise level differences, probably due to the extra characters the interpolation engine needs to handle). Straight up concatenation is about 66% of the speed, which is no great shock. The interpolation parser will look, find nothing to do, then finish with a simple internal string concat. Even if the concat were expensive, the interpolator will still have to do it, after all the work to parse out the variable and trim/copy up the original string.

Surely you can't beat a real life test. But this artificial frankenstein has nothing in common with the real life conditions.
–
Your Common SenseMar 16 '12 at 10:04

1

Those skeptics trying to reproduce these results (like me ;-) on PHP5+ change the microtime() calls to microtime(true) - you want the time as a float, not as some sort of weird string.
–
Erwin WesselsMay 3 '13 at 5:14

@Paolo Begantino: do you actually have any proof of this? phpbench.com respectfully disagrees with you every time I load it.
–
A. RexJan 27 '09 at 5:27

5

Note that even if you use a single-quoted string, PHP is still parsing over every character in it, to look for escape sequences, etc. PHP is also parsing the entire file, so at best, you're still looking at O(n) for the length of the string.
–
dumaJan 19 '11 at 16:58

I've just done some quick testing and there's not much of a saving between these two - certainly nowhere near as much as changing interpolation to concatenation - but single quotes are faster.
–
Colonel SponszNov 27 '09 at 16:54

Don't get too caught up on trying to optimize string operations in PHP. Concatenation vs. interpolation is meaningless (in real world performance) if your database queries are poorly written or you aren't using any kind of caching scheme. Write your string operations in such a way that debugging your code later will be easy, the performance differences are negligible.

@uberfuzzy Assuming this is just a question about language minutia, I suppose it's fine. I'm just trying to add to the conversation that comparing performance between single-quote, double-quote and heredoc in real world applications in meaningless when compared to the real performance sinks, such as poor database queries.

Don't waste time on micro-optimizations like this. Use a profiler to measure the performance of your application in a real world scenario and then optimize where it is really needed. Optimising a single sloppy DB query is likely to make a bigger performance improvement than applying micro-optimisations all over your code.

there is a difference when concatenating variables... and what you are doing with the result... and if what you are doing is dumping it to output, is or isn't output buffering on.

also, what is the memory situation of the server? typically memory management on a higher level platform is worse than that at lower platforms...

$a = 'parse' . $this;

is managing memory at the user code platform level...

$a = "parse $this";

is managing memory at the php system code platform level...

so these benchmarks as related to CPU don't tell the full story.

running the benchmark 1000 times vs running the benchmark 1000 times on a server that is attempting to run that same simulation 1000 times concurrently... you might get drastically different results depending on the scope of the application.