I've attached 4 scripts to benchmark each of the various methods of echoing content. I was very surprised to find that the state of things is completely out of synch with what many of us have come to expect. That said, I trust these benchmarks. Output has been redirected to /dev/null so that rendering to terminal is not an source of benchmark noise. I ran each twice because some things were just so surprising and I ran for PHP4 after running for PHP5 to contrast. So anyways... the benchmarks: shell:~> php5 -v PHP 5.2.5 (cli) (built: Aug 9 2008 01:24:36) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies with eAccelerator v0.9.5.2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator shell:~> time php5 -qC test.echo.php > /dev/null real 1m2.867s user 0m14.285s sys 0m48.043s shell:~> time php5 -qC test.concat.php > /dev/null real 0m15.453s user 0m5.664s sys 0m9.785s shell:~> time php5 -qC test.interpol.php > /dev/null real 0m15.098s user 0m5.736s sys 0m9.353s shell:~> time php5 -qC test.heredoc.php > /dev/null real 0m15.356s user 0m5.020s sys 0m10.333s shell:~> time php5 -qC test.echo.php > /dev/null real 1m1.689s user 0m14.317s sys 0m47.371s shell:~> time php5 -qC test.concat.php > /dev/null real 0m15.414s user 0m5.888s sys 0m9.493s shell:~> time php5 -qC test.interpol.php > /dev/null real 0m15.120s user 0m5.488s sys 0m9.625s shell:~> time php5 -qC test.heredoc.php > /dev/null real 0m15.250s user 0m5.576s sys 0m9.665s Wow! Look at the time it takes for echo to run with multiple parameters. It's about 4 times longer than any other option. And all the other options are almost on perfect par. Considering this was interated 10 million times I don't think there's any reason to choose one method over another when considering concatenation, interpolation, or heredoc. But this is weird. We were told that echo was fastest a long time ago and heredoc was slowest. I'm not sure why multiple parameter echo is so piggishly slow but I'm guessing optimizations were done for interpolation and heredoc to make them on par with concatenation. But let's take a peek back to PHP4 to see what the same scripts do there: shell:~> php -v PHP 4.4.9 (cli) (built: Aug 9 2008 01:27:42) Copyright (c) 1997-2008 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies with eAccelerator v0.9.5, Copyright (c) 2004-2006 eAccelerator, by eAccelerator shell:~> time php -qC test.echo.php > /dev/null real 1m8.570s user 0m20.961s sys 0m47.591s shell:~> time php -qC test.concat.php > /dev/null real 0m19.879s user 0m10.201s sys 0m9.657s shell:~> time php -qC test.interpol.php > /dev/null real 0m28.446s user 0m18.469s sys 0m9.973s shell:~> time php -qC test.heredoc.php > /dev/null real 0m28.136s user 0m18.465s sys 0m9.665s shell:~> time php -qC test.echo.php > /dev/null real 1m10.657s user 0m21.421s sys 0m49.223s shell:~> time php -qC test.concat.php > /dev/null real 0m19.578s user 0m10.073s sys 0m9.501s shell:~> time php -qC test.interpol.php > /dev/null real 0m28.729s user 0m18.681s sys 0m10.021s shell:~> time php -qC test.heredoc.php > /dev/null real 0m27.875s user 0m17.913s sys 0m9.961s Again we see that echo with multiple params sucks. But now we see the old expectations for interpolation and heredoc... though interestingly heredoc is pretty much on par with double quote interpolation. So it seems from PHP4 to PHP5 (current versions anyways) that interpolation and heredoc got a big boost to make them on par with concatenation. So I guess there's not much point any longer on being preachy about efficiency between these three options :) For those wondering btw, eAccelerator is benign for shell execution. That echo benchmark though... WTF! Scripts are attached for your own testing / commenting about how much they suck ;) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP
Attachment:
test.echo.php
Description: application/php
Attachment:
test.concat.php
Description: application/php
Attachment:
test.interpol.php
Description: application/php
Attachment:
test.heredoc.php
Description: application/php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php