News:

Wondering if this will always be free?  See why free is better.

Main Menu

echo in PHP 5 ...

Started by Joshua Dickerson, August 27, 2005, 02:53:21 AM

Previous topic - Next topic

Joshua Dickerson

Quote from: http://us2.php.net/manual/en/function.echo.php#53925The benchmark notes from 2002 don't seem to be right any more. I did some loop tests for different kinds of output formatting in PHP 5.0.4. The numbers are the time taken for 10000 iterations:

echo 'Key is ', $k, '<br>',"\n",'Value is ', $v, '<br><br>', "\n";
0.97371506690979
echo 'Key is ', $k, "<br>\nValue is ", $v, '<br><br>', "\n";
0.77851700782776
echo "Key is $k<br>\nValue is $v<br><br>\n";
0.51984405517578
echo 'Key is '. $k. '<br>'."\n".'Value is '. $v. '<br><br>'. "\n";
0.37696409225464

It seems that concatenation is way faster than multiple parameters, and interpolation is pretty good too.
Anyone to try this out?
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

[Unknown]

Those tests don't fit with my tests on PHP 5.

On relatively long strings, and running the test quite a few times:

echo $GLOBALS['x'], ' test ', $GLOBALS['y'], $GLOBALS['z'], ' test ';

Takes: 0.13236, 0.13457, 0.13493, 0.13688 seconds.

And:

echo $GLOBALS['x'] . ' test ' . $GLOBALS['y'] . $GLOBALS['z'] . ' test ';

Takes: 0.58292, 0.58244, 0.60418, 0.57117 seconds.

Clearly, concatenation is slower for real-world strings.  For smaller strings: (more times)

echo $GLOBALS['x'], ' test ', $GLOBALS['y'], $GLOBALS['z'], ' test ';

Takes: 0.11807, 0.12496, 0.13415, 0.11612 seconds.

While:

echo $GLOBALS['x'] . ' test ' . $GLOBALS['y'] . $GLOBALS['z'] . ' test ';

Takes: 0.32622, 0.33226, 0.33109, 0.32855 seconds.

So, even, in the case of short strings, concatenation is slower (although not as much slower.)

I don't doubt that the quoted test was done incorrectly.  I can provide the source code for my benchmarking system, which currently includes the following tests, some of which require PHP 5:

    * create_function1: create_function() vs. eval()
    * echo1: output with commas vs. concat on long strings
    * echo2: output with commas vs. concat on short strings
    * echo3: output with commas vs. interpolation on long strings
    * echo4: output with commas vs. interpolation on short strings
    * foreach1: foreach reference reading
    * foreach2: foreach reference writing
    * foreach3: foreach $k => $dummy vs. array_keys()
    * foreach4: foreach vs. while
    * include1: include vs. require
    * include2: include vs. eval
    * shootout_random: random benchmark http://shootout.alioth.debian.org/
    * strtr1: strtr() vs. str_replace() on long strings
    * strtr2: strtr() vs. str_replace() on short strings

-[Unknown]

Joshua Dickerson

#2
Perhaps a note in the PHP docs?

How about a post or board for benchmarks? The problem with PHP is that there are too many ways to do the same thing. Perhaps if we figured out the fastest (and most likely prettiest) ways to do things, it would help programmers here. Which may include mod writers and maybe even future developers.
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

[Unknown]

A whole board for benchmarks?  Isn't that a bit much?  Maybe a whole board for each of the 30 converters too?

A topic makes more sense, but no one ever agrees.  It's like the copyrights topic, everyone will use it as their personal spitting ground; they don't really matter, they'll say.  My crappy benchmark that does it 10 times while I'm zipping a file says fsockopen is faster than fopen, etc.

-[Unknown]

Joshua Dickerson

* groundup notes his powers of moderation ;)

So, can you post your results from the above benchmarks?
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Advertisement: