|
Posted by David Haynes on 05/30/06 11:27
Berimor wrote:
> 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
Well, I have a few moments to play right now...
Some quick empirical tests:
[foo1.php]
<?php
for($i=0; $i < 1000000; $i++ ) {
echo "foo\n";
}
?>
[foo2.php]
<?php
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
Notes:
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.
1,000,000
foo3 R 2.136
U 2.068
S 0.068
and for foo2.php
1,000,000
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.
-david-
Navigation:
[Reply to this message]
|