Hashar noticed that for simple string literals, single quotes are faster
than double quotes, if you count both parse time and execution time.
This is reasonably easy to understand: single quotes have only two
special characters, \ and ', so the loop body to process them would be
simpler. Some people went on to assert that by extension $foo.' '.$bar
is faster than "$foo $bar", without doing any benchmarks. I was never
convinced of this.
I did a benchmark where I had one file which looked like this:
<?php
$foo = 'foo';
$bar = 'bar';
echo "
$foo $bar $foo $bar $foo $bar $foo $bar $foo $bar $foo $bar $foo $bar
...
?>
And one file that looked like this:
<?php
$foo = 'foo';
$bar = 'bar';
echo
$foo.' '.$bar.' '.$foo.' '.$bar.' '.$foo.' '.$bar.' '.$foo.' '.$bar.
...
?>
There were 10000 "foo bar" strings output by each one. On my laptop,
with no bytecode caching, string interpolation was 12 times faster than
concatenation.
Document Path: /test/concatenation.php
Document Length: 80001 bytes
Concurrency Level: 1
Time taken for tests: 7.420670 seconds
Complete requests: 10
Failed requests: 0
Write errors: 0
Total transferred: 801670 bytes
HTML transferred: 800010 bytes
Requests per second: 1.35 [#/sec] (mean)
Time per request: 742.067 [ms] (mean)
Time per request: 742.067 [ms] (mean, across all concurrent requests)
Transfer rate: 105.38 [Kbytes/sec] received
Document Path: /test/string_interpolation.php
Document Length: 82242 bytes
Concurrency Level: 1
Time taken for tests: 0.610879 seconds
Complete requests: 10
Failed requests: 0
Write errors: 0
Total transferred: 824080 bytes
HTML transferred: 822420 bytes
Requests per second: 16.37 [#/sec] (mean)
Time per request: 61.088 [ms] (mean)
Time per request: 61.088 [ms] (mean, across all concurrent requests)
Transfer rate: 1316.14 [Kbytes/sec] received
The slightly longer filesize in the string_interpolation.php case was
because I used line breaks embedded in the string to break up powers of
10, whereas in concatenation.php the line breaks were not in the strings.
The main reason I'm doing this is because I think $foo.' '.$bar looks
ugly compared to "$foo $bar", and it also takes up more screen space. I
doubt the performance gain would be significant either way,
concatenation takes only 19us. I just don't like seeing the MediaWiki
codebase uglied up for specious reasons.
-- Tim Starling