On Sat, Jul 29, 2006 at 12:19:03PM -0600, Chad Perrin wrote:
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.
Curiosity got the better of me, so I did a quick, unscientific benchmark
using the unix time utility. Results, with script output cut out, on a
script that prints "foo bar" 1499 times, once with comma separation,
once with string concatenation, and once with a single interpolated
string:
$ time php -f comma.php
real 0m0.546s
user 0m0.151s
sys 0m0.070s
$ time php -f concat.php
real 0m0.351s
user 0m0.173s
sys 0m0.025s
$ time php -f interp.php
real 0m0.286s
user 0m0.108s
sys 0m0.025s
So, apparently, I got the opposite of what I would have expected. I
think the PHP interpreter is actually running a separate echo routine
for every comma-separated string. Interpolation is fastest.
I didn't add any linebreaks or spaces for code readability, so this is
about as direct a performance comparison as I'm likely to get from the
time utility, and this data set, I suppose.
--
CCD CopyWrite Chad Perrin [
http://ccd.apotheon.org ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of
others we should be glad of an Opportunity to serve others by any
Invention of ours, and this we should do freely and generously."