Alex Rivera | Logout

PHP: many concats or one implode?

Asked 2010-12-21T18:32:07.777
15

In my current project I assemble one string out to many small strings (direct output is NOT an option). Would it be more efficient to do many string concatenations? or should I add the parts to an array and implode it?

Edit
Report

1 Answer

9

The optimization would be little compared to network traffic, databases, files, graphics, etc. However, here is a reference on the topic from sitepoint.

http://www.sitepoint.com/high-performance-string-concatenation-in-php/

Which is fastest?

The good news is that PHP5 is quick. I tested version 5.3 and you’re far more likely to run out of memory than experience performance issues. However, the array implode method typically takes twice as long as the standard concatenation operator. A comparable period of time is required to concatenate the string or build the array, but the implode function doubles the effort.

Unsurprisingly, PHP is optimized for string handling and the dot operator will be the fastest concatenation method in most cases.

answered 2010-12-21T18:39:15.613

Your Answer