KBD

Keith Devens .com

Friday, March 19, 2010 Flag waving
Don't be an asshole. – Jason Hoffman
← 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

Php (http://www.benivolent.com) wrote:

If absolute fastest use output buffering and use buffer into variable the string concatenating is slightly faster.

∴ Php | 23-Jul-2009 4:53am est | http://www.benivolent.com | #11277

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)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



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

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.117s.

(Used 8 db queries)