Keith Devens .com |
Thursday, July 2, 2009 | ![]() |
| If we wish to count lines of code, we should not regard them as *lines produced* but as *lines spent*. – Edsger Dijkstra | ||
|
| ← Playing with MandrakeMove | Updated libraries → |

Adam Langley (http://www.imperialviolet.org) wrote:
Joshua Paine (http://turtle.fairsky.us/) wrote:
The absolute fastest is to use output buffering. Start a buffer, echo/print everything to output, then pour the buffer into a variable. Slightly but noticeably faster than concatenating--strange but true.
Keith (http://keithdevens.com/) wrote:
Thanks Joshua, I'll have to time it that way as well and publish my results.
Keith (http://keithdevens.com/) wrote:
Hey Joshua, you were right. Not as much of a difference from concatenating to output buffering as it was from building arrays to concatenating, but it did make a difference. Plus, it's often less to type as well. Thanks.
John Lim (http://php.weblogs.com/) wrote:
Hi Keith,
Neat trick. I was intrigued enough to check the PHP source code.
A look at output.c shows that ob_start() allocates an enormous 40K buffer for strings. And each time the buffer needs to grow, it does it in chunks of 10K.
John
Erik wrote:
A noob request, but could you show the source code as to how output buffering is done?
The absolute fastest is to use output buffering. Start a buffer, echo/print everything to output, then pour the buffer into a variable
Keith (http://keithdevens.com/) wrote:
No problem. The best thing for me to do is point you to my XML library (source here). I use this technique in the XML_serialize function.
The relevant lines are as follows:
<?php
ob_start();
#output here
$str = ob_get_contents();
ob_end_clean();
return $str;
?>
Or, if you just want to output the string rather than returning it, you can replace ob_get_contents() and ob_end_clean() with ob_end_flush(). You could also replace the combination of ob_get_contents() and ob_end_clean() with ob_get_clean() if you're using PHP 4.3.0 or above. See PHP's documentation at http://php.net/outcontrol
Claudex (http://apsique.virtuabyte.cl/php/) wrote:
With 100 iterations on my Athlon 2000XP, with Windows Xp:
Append:0.001133
Implode:0.003901
OB:0.000870
213.192.71.190 wrote:
100 operations? Is that supposed to be funny?
Do at least a billion - counting microseconds is stupid - unless you write "0.001 +/- 100%".
Yes, the error can be this big.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):
Generated in about 0.187s.
(Used 8 db queries)
Though this is not true in Python.
AGL