Page 1 of 1

Output buffering

Posted: Sun May 01, 2011 11:41 pm
by Netroxy
I'm hearing that using output buffering speeds up your web page to load efficiently. It helps the output when it is sent.

I am just wondering if using 'ob_start();' at the beginning and 'ob_end_flush());' at the end of any script would do anything to speed things up? Like this:

<?php
ob_start();


All PHP code goes in here


ob_end_flush();
?>

Re: Output buffering

Posted: Mon May 02, 2011 1:11 am
by McInfo
I did a test using two files. One was like the code posted below. The other was identical except that it excluded the output buffering functions.

Code: Select all

<?php
include 'Timer.php';
$timer = new Timer(__FILE__.'.log');
$timer->start();

ob_start();

for ($i = 0; $i < 100000; ++$i) {
    echo $i, ' ';
}

ob_end_flush();

$timer->stop();
Timer is a proprietary class that basically keeps track of microtime() and stores the difference between start and stop times in a log file. None of its methods used in the test scripts generate output.

I executed the two scripts alternately (a, b, a, b, ...) for a total of 25 trials. Each script output about half a megabyte of data each trial (actually 588890 bytes, as measured in the browser).

This is a summary of the results. The numbers are execution times in seconds.

Code: Select all

+-----+----------+----------+----------+
| ob  | average  | min      | max      |
+-----+----------+----------+----------+
| off | 0.145272 | 0.063476 | 0.205439 |
| on  | 0.138644 | 0.060136 | 0.183473 |
+-----+----------+----------+----------+
According to the data, on average, output buffering improved the execution time by almost 4.6%. I suppose that means the answer to your question is "yes" to a small degree.

Here are the actual results.

Code: Select all

+-------+----------+----------+
| trial | ob off   | ob on    |
+-------+----------+----------+
|     0 | 0.108065 | 0.062686 |
|     1 | 0.098824 | 0.144998 |
|     2 | 0.191003 | 0.127031 |
|     3 | 0.101148 | 0.140355 |
|     4 | 0.135300 | 0.150780 |
|     5 | 0.126131 | 0.147001 |
|     6 | 0.157830 | 0.123407 |
|     7 | 0.191019 | 0.147902 |
|     8 | 0.146561 | 0.127883 |
|     9 | 0.172577 | 0.062201 |
|    10 | 0.118603 | 0.161549 |
|    11 | 0.132560 | 0.134461 |
|    12 | 0.180060 | 0.060136 |
|    13 | 0.205439 | 0.178002 |
|    14 | 0.143739 | 0.167857 |
|    15 | 0.178421 | 0.175938 |
|    16 | 0.158866 | 0.116499 |
|    17 | 0.181381 | 0.118909 |
|    18 | 0.102528 | 0.165416 |
|    19 | 0.161982 | 0.173608 |
|    20 | 0.101873 | 0.167801 |
|    21 | 0.112579 | 0.161971 |
|    22 | 0.063476 | 0.131357 |
|    23 | 0.196879 | 0.134874 |
|    24 | 0.164948 | 0.183473 |
+-------+----------+----------+

Re: Output buffering

Posted: Mon May 02, 2011 8:40 am
by Netroxy
Thats a good log right there. Shows slight improvements. I am just wondering if it is possible to call ob_start(); twice without closing the buffer? Maybe like nesting the buffers?

Re: Output buffering

Posted: Mon May 02, 2011 10:22 am
by McInfo
PHP Manual: [url=http://www.php.net/manual/en/function.ob-start.php]ob_start()[/url] wrote:Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.