CodeIgniter Forums
Overhead of echo? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Overhead of echo? (/showthread.php?tid=19581)

Pages: 1 2


Overhead of echo? - El Forum - 06-11-2009

[eluser]slowgary[/eluser]
Howdy Ya'll

I was just wondering how output buffering works in CI. Traditionally, I have tried to avoid multiple echo calls in my PHP scripts by using a variable, like so:
Code:
$output = '<ul>';

foreach(array)
{
     $output .= '<li>';
}

$output .= '</ul>';


echo $output;

I always assumed this was more efficient than calling multiple echos, even though it's possible that it consumes more memory. Is this actually more efficient?

In the case of CI, everything goes to an output buffer first. I've found myself reverting in my views to just using echo everywhere. I'm sure it still has the overhead of calling echo though. I know echo is a construct as opposed to print which is a function, but I'm not even sure what that means. I know echo is faster than print. Should I still be using a variable instead of multiple echos? Does it not matter in CI? Was it wrong in the first place? Where do babies come from?

Thanks for reading.


Overhead of echo? - El Forum - 06-11-2009

[eluser]skunkbad[/eluser]
I don't think it matters, but I always use a variable. I had problems with the variable named $output, and although the docs don't say it's a reserved variable name, I had to use another variable name. I use $contents.


Overhead of echo? - El Forum - 06-11-2009

[eluser]slowgary[/eluser]
Your $output may have been unrelated to CI, I just tried to echo and assign it and both worked ok.

I know in the grand scheme of things, this might be a nitpicky issue. In some cases though if there are hundreds of echos per page load it may make a difference. I just like to know the nitty gritty just to squeeze every last bit of juice from the orange. Plus, sometimes I just like to know the nitty gritty.


Overhead of echo? - El Forum - 06-12-2009

[eluser]mddd[/eluser]
There is $this->output, to if you make $output a property of a class, that will likely cause problems.

On the issue of 'echo', before I used CI, I often built a $output string before outputting stuff from a script.
In CI, I think it is more important to keep my views as clean as possible, so I prefer using 'echo' in stead of
building strings.


Overhead of echo? - El Forum - 06-12-2009

[eluser]Dam1an[/eluser]
I prefer to avoid lots of string concatentation, but this is a habit from my old days as a Java developer, where string concatenation has a high cost, not sure if it's the same case with PHP though.

At work, we managed to get a script down from 40 minutes to 20 seconds by getting rid of all string concatentation Smile (It wasn't my script)


Overhead of echo? - El Forum - 06-12-2009

[eluser]TheFuzzy0ne[/eluser]
I think we need to run some tests to put Gary's mind at ease. Sadly, I don't have time to run any tests right now.


Overhead of echo? - El Forum - 06-12-2009

[eluser]mddd[/eluser]
Outputting text is such a basic thing, I really can't image building up strings being quicker then letting php output it directly.
Especially because the size of the string is changing, so php may not store it in the same place every time.


Overhead of echo? - El Forum - 06-12-2009

[eluser]slowgary[/eluser]
Silly me. I always ask questions first and think later. I hadn't even considered just testing it out. So I for()'ed a million lines of:
Code:
$output .= 'hello ';
Then I tried:
Code:
echo 'hello ';
and then for good measure:
Code:
print 'hello ';


It turns out print and echo performed exactly the same. The string concatenation method used a little more memory and took a little longer to run.

I'm happy! It's like my birthday! I can just echo freely all over the place. Sweet. I too prefer the cleaner look of view files when I simply echo everything.

Thanks guys.


Overhead of echo? - El Forum - 06-12-2009

[eluser]TheFuzzy0ne[/eluser]
Don't forget that if you're using a view, CodeIgniter will chuck a wobbler if you send output before the headers, and if you're echoing from within a view, then it's buffered and stored in a variable anyway.


Overhead of echo? - El Forum - 06-12-2009

[eluser]slowgary[/eluser]
Hmmm... I thought something smelled fishy. I tried it again outside of CI and ran each loop 5 times.

Here's what I came up with:
===========================

echo: 2.13463592529 | 2.19948506355 | 2.65301418304 | 3.14114904404 | 2.65329194069

print: 2.02593612671 | 2.54435801506 | 2.82758998871 | 3.81649684906 | 4.0810148716

$buffer: 0.336583852768 | 0.341640949249 | 0.3435151577 | 0.360665798187 | 0.361016988754


The numbers stagger a little (shared hosting), but the answer's still good news. The $buffer method is way faster, but since CI has it's own output buffering, we can echo all over town and still see the performance benefits of using a buffer. Sweet!