Skip navigation.

Yea.. nice example Chris, maybe next you'll be right!

Bad Architecture

There's this nagging voice in the back of my head that says, "don't make fun of the readers! They anger easy and won't come back". However, I couldn't resist reponding to Chris's post about using arrays to create strings because it's faster than concatenation!

So this is today's WTF:

You do realize that cating strings is slower than manipulating arrays, right?
// meh...
/* -->8-- */
// update the area code
$stSQL .= "$areacode=$codeData, ";

// update the phone num
$stSQL .= "$phonenum='$phoneData' ,";
/* -->8-- */

// yay!
/* -->8-- */
// update the area code
$update_array[] = "$areacode=$codeData";

// update the phone num
$update_array[] = "$phonenum='$phoneData'";

$query = "UPDATE tablename SET " . implode(', ', $update_array) ....;
/* -->8-- */

And of course, you should always be checking to see if a variable actually exists, rather than simply assuming it will be there (*cough*$_POST[$areacode]*cough*). I hope this is just an oversight while making an example of poor code.

But from this little shell script we find that string concatenation is more than twice as fast than using arrays.

Run Number #1
1199999 - bigString, Time: 0.5843870639801
1199999 - bigString2, Time: 1.0092749595642
Run Number #2
1199999 - bigString, Time: 0.34683203697205
1199999 - bigString2, Time: 1.0810549259186
Run Number #3
1199999 - bigString, Time: 0.35090112686157
1199999 - bigString2, Time: 1.0080449581146

Well if you look at the shell script code you'll see I do 100,000 iterations. But if you only do like 20 iterations using an array is actually faster, take a look:

239 - bigString, Time: 0.0015630722045898
239 - bigString2, Time: 0.0010149478912354

Only after about 150 iterations does string concatenation become faster than using an array. See:

1799 - bigString, Time: 0.0024600028991699
1799 - bigString2, Time: 0.0025348663330078

So yea he's right, but then again, who the hell cares about those thousandths of a second? At least I'll be right when generating SQL for tables with more than a few hundred columns! ;b


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Turns out that perhaps both methods are wrong for large strings. The fastest method to create large strings is through output buffering!


inside the implode function, must exist a loop implemented with C, and a concatenation. It's almost obviously that is slower.

Uhm...hmkaaaay, but why would u wanna use output buffering when there is nothing to output? o_O Or did I miss the point?

There's no echo, print or similiar used.

Should have read the site more carefully, I did miss the point. But the discussion if its better to allocate 40k of memory for concatenating strings instead of simple concatenate them could be endless... at least its useless here, cause we are talking about ... uhm... what...? A few bytes?

THIS form of string concatenating is polynomially slower.


Dudes this is pretty fast... tested it and the code completed within O(1). Should mention that I got some stupid error-message generated by PHP instead....doh! I wonder what it is that makes programming the tough job it is.

there's more too crappy coding than performance..

this example, tho perhaps fast, is just total crap.

This is the most ridiculous waste of time I've ever seen. The real question is not WTF - the real question is WGAS: Who Gives A S***. All the opensores PHP projects i've seen randomly mix presentation, data access and business logic, use poorly contstructed objects for no good reason (just so they can say they used objects it seems), and employ idiotic templating systems that suck up resources. (Hint: using a templating system that supports loops and conditionals that can quickly become complicated is not getting you any farther than using PHP.) Look, why don't you kids work on solid programming practices and cleaning up that garbage before splitting hairs over arrays vs. string concats. Regards, Bartleby

Interesting, but you forgot to test with the same output.

The concatenation test finishes with "item1, item2, " where as the array test finishes with "item1, item2". You'd have to do a substr() after concatenating to get the same results, which is going to add a bit more time (though I'm not sure how much)

Post new comment


  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br>
  • Web and e-mail addresses are automatically converted into links.