[PHP] Benchmark for echoing via comma, concat, inteprolation, and heredoc.

2008-08-27 Thread Robert Cummings
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

real1m2.867s
user0m14.285s
sys 0m48.043s

shell:~ time php5 -qC test.concat.php  /dev/null

real0m15.453s
user0m5.664s
sys 0m9.785s

shell:~ time php5 -qC test.interpol.php  /dev/null

real0m15.098s
user0m5.736s
sys 0m9.353s

shell:~ time php5 -qC test.heredoc.php  /dev/null

real0m15.356s
user0m5.020s
sys 0m10.333s

shell:~ time php5 -qC test.echo.php  /dev/null

real1m1.689s
user0m14.317s
sys 0m47.371s

shell:~ time php5 -qC test.concat.php  /dev/null

real0m15.414s
user0m5.888s
sys 0m9.493s

shell:~ time php5 -qC test.interpol.php  /dev/null

real0m15.120s
user0m5.488s
sys 0m9.625s

shell:~ time php5 -qC test.heredoc.php  /dev/null

real0m15.250s
user0m5.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

real1m8.570s
user0m20.961s
sys 0m47.591s

shell:~ time php -qC test.concat.php  /dev/null

real0m19.879s
user0m10.201s
sys 0m9.657s

shell:~ time php -qC test.interpol.php  /dev/null

real0m28.446s
user0m18.469s
sys 0m9.973s

shell:~ time php -qC test.heredoc.php  /dev/null

real0m28.136s
user0m18.465s
sys 0m9.665s

shell:~ time php -qC test.echo.php  /dev/null

real1m10.657s
user0m21.421s
sys 0m49.223s

shell:~ time php -qC test.concat.php  /dev/null

real0m19.578s
user0m10.073s
sys 0m9.501s

shell:~ time php -qC test.interpol.php  /dev/null

real0m28.729s
user0m18.681s
sys 0m10.021s

shell:~ time php -qC test.heredoc.php  /dev/null

real0m27.875s
user0m17.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


test.echo.php
Description: application/php


test.concat.php
Description: application/php


test.interpol.php
Description: application/php


test.heredoc.php
Description: application/php
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: [PHP] Benchmark for echoing via comma, concat, inteprolation, and heredoc.

2008-08-27 Thread tedd

At 12:35 PM -0400 8/27/08, Robert Cummings wrote:

-big snip-

That echo benchmark though... WTF!

Scripts are attached for your own testing / commenting about how much
they suck ;)

Cheers,
Rob.



Way to go Rob -- however, we have to realize that this is one of 
those things that only Geeks get excited about.


The rest of the world meanders along not knowing what we've discovered.

Oh, the ignorant fools -- if they only realized the knowledge and 
power we hold in the palm of our collective hands and appreciated how 
we administer this for the greater good of all. Someday, they might 
realize our contributions and build statues to our heroic efforts -- 
or do you think I'm going too far with this?


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php