On Sat, Jul 29, 2006 at 08:29:31PM +1000, Tim Starling wrote:
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'd never bothered to consider whether concatenation or string
interpolation was slower -- I just knew that both would slow things
down. Nice to know someone checked on it, at least for academic
purposes.
I did a benchmark where I had one file which looked like this:
[ snip a bunch of data and explanation ]
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.
Excellent point.
However . . .
This is almost certainly the fastest option, when using echo (which is
faster than print, if I'm not mistaken, though it doesn't work as part
of a more complex enclosing expression):
echo $foo, ' ', $bar;
One can feed multiple strings to echo for printing via the comma without
having to wait through the concatenation operation, similarly to the way
the print function works in Perl. Of course, I'm guessing: for all I
know, PHP might be so obtuse as to treat multiple arguments to echo like
a loop, and restart an expensive operation for every argument.
Considering I don't really use PHP when speed is a concern, though, it's
unsurprising I haven't bothered to benchmark it.
--
CCD CopyWrite Chad Perrin [
http://ccd.apotheon.org ]
"There comes a time in the history of any project when it becomes necessary
to shoot the engineers and begin production." - MacUser, November 1990