Right output ...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi guys,

just wondering - which method of output is more fast and use less system  
resources. The page being build with outup from multiple functions. So, is  
it better to output each function result directly with "echo" construction:

echo "string1";
echo "string2";
echo "stringN";

or maybe better to collect all outputs in a variable first:

$out.= "string1";
$out.= "string2";
$out.= "stringN";
echo $out;

Maybe someone made a kind of research in this field :)

Web Design Essex | Multimedia | Printing  http://nextwave.co.uk

Re: Right output ...

Berimor wrote:
Quoted text here. Click to load it

Well, I have a few moments to play right now...

Some quick empirical tests:
for($i=0; $i < 1000000; $i++ ) {
         echo "foo\n";

for( $i=0; $i < 1000000; $i++) {
   $foo .= "foo\n";
echo $foo;

Running for 100,000 and 1,000,000 cycles produces:

         100,000  1,000,000
foo   R  0.213     1.317
       U  0.196     1.292
       S  0.016     0.024

foo1  R  1.156    10.590
       U  0.704     5.708
       S  0.452     4.876

foo2  R  0.540     5.887
       U  0.388     3.760
       S  0.152     2.128

R - Wall clock, S - Time in the O/S, U - Time in the user program
All times are in seconds and were taken with the Gnu 'time' command.
The 'foo' process performs the loop but does not echo or concatenate. It  
presents the base overhead of invoking php and doing the loop.
All output was sent to /dev/null to remove any display buffer lag.

As you can see, concatenation is much lighter weight than echoing.

I then changed foo1.php to include an ob_start() and ob_flush() set.

foo3  R  2.136
       U  2.068
       S  0.068

and for foo2.php

foo4  R  5.746
       U  3.760
       S  1.988

The moral seems to be that if you care about output speed, using the  
output buffer with smaller data pieces appears to be your best bet.


Site Timeline