KBD

Keith Devens .com

Friday, July 4, 2008 Flag waving
Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will... – Eric S. Raymond
← Playing with MandrakeMoveUpdated libraries →

Daily link icon Friday, May 28, 2004

Building large strings in PHP

Note to PHP programmers: It turns out it's much faster to build large strings from many smaller strings by concatenating them onto one string than it is to append to an array repeatedly and then join that array into a string. I timed it and was surprised by the results.

← Playing with MandrakeMoveUpdated libraries →

Comments XML gif

Adam Langley (http://www.imperialviolet.org) wrote:

Though this is not true in Python.

AGL

∴ Adam Langley | 29-May-2004 5:02am est | http://www.imperialviolet.org | #4697

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.

∴ Joshua Paine | 30-May-2004 7:19pm est | http://turtle.fairsky.us/ | #4704

Keith (http://keithdevens.com/) wrote:

Thanks Joshua, I'll have to time it that way as well and publish my results.

Keith | 31-May-2004 9:50pm est | http://keithdevens.com/ | #4706

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.

Keith | 4-Jun-2004 7:32pm est | http://keithdevens.com/ | #4730

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

∴ John Lim | 7-Jun-2004 11:27am est | http://php.weblogs.com/ | #4735

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

∴ Erik | 7-Jun-2004 11:01pm est | #4739

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

Keith | 7-Jun-2004 11:10pm est | http://keithdevens.com/ | #4740

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

∴ Claudex | 8-Jun-2004 3:36am est | http://apsique.virtuabyte.cl/php/ | #4741

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.

∴ 213.192.71.190 | 5-Oct-2007 9:28am est | #10325

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.113s.

(Used 8 db queries)

mobile phone