output buffering
Moderator: General Moderators
output buffering
I've just looked trough the output-buffering function at php.net, and i got a question. I'm currently using my own template-system for my site, which works, but it's using variables to store the output before echoing it out. i thought that i could use output-buffering instead, so i wouldn't have to "add contents to the output-variable", but instead, echo it, and then use ob_get_contents() and then ob_end_clean() to get the output, put it into a variable, and then echo it. But now onto the question: Would it be better to use the output-buffering, or should i stick to the variable-based system?
A matter of taste (?).
I prefeer ob_* when dealing with alot of header() functions as those are being sendt before any other output. Sometimes it's easier to define a header way down the script(s) that "at the top" as usually recommended. Functionallity might be the same, but you have another way to actually write clean, readable code.
But if it's better than any other way, I'm not sure.
I prefeer ob_* when dealing with alot of header() functions as those are being sendt before any other output. Sometimes it's easier to define a header way down the script(s) that "at the top" as usually recommended. Functionallity might be the same, but you have another way to actually write clean, readable code.
But if it's better than any other way, I'm not sure.
Don't quote me but I think I remember that it is quicker to build strings rather than have many echo statements (buffered or not). I'd have to check my notes which I don't have with me right now.
A possible exception is a site search where I might print each result as it is defined - might be slightly slower overall but, to the user, the script starts providing output more quickly.
Why don't you try a test? There's an example getMicroTime fn in the manual. As with the vast majority of optimisation issues, it's unlikely to make a material difference one way or the other but it's always worth testing.
As Jam mentioned, avoid client output until you're sure all header calls, session_start etc have been carried out.
A possible exception is a site search where I might print each result as it is defined - might be slightly slower overall but, to the user, the script starts providing output more quickly.
Why don't you try a test? There's an example getMicroTime fn in the manual. As with the vast majority of optimisation issues, it's unlikely to make a material difference one way or the other but it's always worth testing.
As Jam mentioned, avoid client output until you're sure all header calls, session_start etc have been carried out.
Used this code:
to test the times, tell me if I should edit anything to get more accurate times. I've tested the code on my own server.
OB-test produced:
0.000367 s
0.000449 s
0.000447 s
0.000436 s
0.000423 s
Variable-test produced:
0.000451 s
0.000488 s
0.000505 s
0.000499 s
0.000468 s
It doesn't seem to be a big difference...
Edit: did a test with a for-loop of 500, this is the results:
OB-test produced:
0.001823 s
0.001125 s
0.001815 s
0.001843 s
0.001955 s
Variable-test produced:
0.002362 s
0.002027 s
0.002171 s
0.001209 s
0.002161 s
Those are some more accurate answers, and the result is that OB seems to be faster...
Code: Select all
<?php
##### execution time starts #####
function utime () {
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
$start_time = utime();
#### /execution time starts #####
if ($_GET['i'] == 'ob') {
########### output buffering ###########
ob_start();
### create some output
echo "Output-buffering example<br />";
for ($i = 1; $i <= 100; $i++) {
echo "<br />$i";
}
# get output
$output = ob_get_contents ();
ob_end_clean();
### final output
echo "<html>
<head>
<title>title</title>
</head>
<body bgcolor='#ABCDEF'>
{$output}
";
### /final output
########## /output buffering ###########
}
else {
########### variable style ###########
### create some output
$output = "Variable example<br />";
for ($i = 1; $i <= 100; $i++) {
$output .= "<br />$i";
}
### final output
echo "<html>
<head>
<title>title</title>
</head>
<body bgcolor='#ABCDEF'>
{$output}
";
### /final output
########## /variable style ###########
}
##### output execution time #####
echo "<br /><br />executed in: ".substr(utime () - $start_time, 0, ." seconds";
?>OB-test produced:
0.000367 s
0.000449 s
0.000447 s
0.000436 s
0.000423 s
Variable-test produced:
0.000451 s
0.000488 s
0.000505 s
0.000499 s
0.000468 s
It doesn't seem to be a big difference...
Edit: did a test with a for-loop of 500, this is the results:
OB-test produced:
0.001823 s
0.001125 s
0.001815 s
0.001843 s
0.001955 s
Variable-test produced:
0.002362 s
0.002027 s
0.002171 s
0.001209 s
0.002161 s
Those are some more accurate answers, and the result is that OB seems to be faster...