Hudzilla.org - the homepage of Paul Hudson
Contents > Output Buffering Wish List | Report Bug | About Me ]

13.1     Advantages

This is NOT the latest copy of this book; click here for the latest version.

Forget having to send your cookies near the start of your page! Output buffering lets you "send" cookies at any point your script - although, of course, it just stores the cookies separately to the HTML data then sends them together at the end, in the correct order. The bulking together of data also provides quite a performance improvement - PHP literally hangs on to all data until you instruct it to send, at which point all data is sent in one chunk.

Once you are using output buffering, you can compress content before you send it. Due to the fact that HTML is lots of very simple, repeating tags, and normal writing on a site is also very easy to compress, compressing your pages can make a big dent in the amount of bandwidth your site (and your visitor!) uses.

One final advantage is that output buffers are stackable, meaning that you can have several buffers working on top of each other, sending to output whichever ones you want.





<< 13 Output Buffering   13.2 Performance Considerations >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Zac - 07 Sep 2008

Your chapter on output buffering is one of the best pieces I've read on the topic. However, I would like to know how you are measuing _performance_ since you say there is a "performance improvement" when saving up the data and kicking it out all at once.

I agree (because of the subtlties of ethernet frame, IP packet and TCP packet fragmentation and reconstruction), that network utilization is better (fewer packets for the same amount of data which decreases network use).

However, I decided to set up a small benchmarking script to test if buffering really made the page faster or more scalable (the two forms of _performance_ that is most critical to high-profiles sites).

On my system (dual PIII running FreeBSD/Apache), I found that output buffering slowed down the same code by 400%. Here's my script:

<?php /** Output Buffering Benchmark Test **/
$started = microtime(true);
if($_REQUEST['doit'] == 'ob') {
ob_start();
print_stuff();
ob_end_flush();
}
else if($_REQUEST['doit'] == 'norm') {
print_stuff();
}
else if($_REQUEST['doit'] == 'string') {
echo get_string();
}
else {
?><p><a href="test.php?doit=ob">Run OB Test</a></p>
<p><a href="test.php?doit=norm">Run Non-OB Test</a></p>
<p><a href="test.php?doit=string">Run String Buffer Test</a></p><?php
}
echo 'Run Time: '.number_format((microtime(true)-$started),8).' seconds.';

function print_stuff() {
echo '<pre>';
for($i = 0; $i < 600; $i++) {
for($j = 0; $j < 60; $j++) { echo '.'; }
echo "\n";
}
echo '</pre>';
}
function get_string() {
$buffer = '<pre>';
for($i = 0; $i < 600; $i++) {
for($j = 0; $j < 60; $j++) { $buffer .= '.'; }
$buffer .= "\n";
}
return($buffer.'</pre>');
}
?>

The results of my tests (averages over 10+ executions):

No buffering: 0.084 seconds
Output Buffering: 0.327 seconds
String Buffering: 0.330 seconds

What I found was actually more dramatic than I expected. I guessed that OB would be a little slower--maybe double in a really bad situation. But, I didn't guess that it would be nearly 4 times slower.

The last tes



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow