468,247 Members | 1,328 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

Right output ...

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
May 30 '06 #1
1 1070
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-

May 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Paul Kooistra | last post: by
22 posts views Thread by Simon | last post: by
3 posts views Thread by deanfamily11 | last post: by
5 posts views Thread by Pioneer | last post: by
2 posts views Thread by johkar | last post: by
4 posts views Thread by Rich Shepard | last post: by
1 post views Thread by anitawa | last post: by
52 posts views Thread by marc | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.