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?
-
1I generally implode if there is a common glue (i.e.; space, return-carriage) – John Giotta Dec 21 '10 at 18:37
-
So why do user lnaguages provide tools to create large strings? Why is there a StringBuilder in C#? Because if you are dealing with large strings the way you build them IS an issue. PHP can not only be used to generate web-pages! Please stop flamming. – Oliver A. Dec 21 '10 at 18:59
-
The best way to optimize large string is to **avoid** large strings. For the web page that is being sent over network and seen by single human, all strings should be short. – Your Common Sense Dec 21 '10 at 19:11
-
4Even it is has a low impact on the code it is one thing more I know. There is no useless knowledge, because anything that improves your understanding on the language you work with is good for you. Please stop flamming. – Oliver A. Dec 21 '10 at 20:40
4 Answers
First a side note - any of this does not matter in a real production application, as the time differences are superficial and the optimization of the application should be done in other places (dealing with network, database, filesystem, etc.). That being said, for curiosity's sake:
implode
may be more efficient concatenation, but only if you already have the array. If you don't, it probably would be slower since all the gain would be offset by the time needed to create the array and allocate its elements. So keep it simple :)

- 10,593
- 6
- 56
- 103
-
Thank you. I know there are other more import issues,but I will have to use this method all over my project, so I thought I should ask and find the most efficent way :) – Oliver A. Dec 21 '10 at 18:47
<?php
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
define('ITERATIONS', 10000);
header('Content-Type: text/plain');
printf("Starting benchmark, over %d iterations:\r\n\r\n", ITERATIONS);
print("Imploding...");
$start = microtime_float();
$list = Array();
for ($_ = 0; $_ < ITERATIONS; $_++)
$list[] = 'a';
$result = implode('',$list);
$end = microtime_float() - $start;
printf("%0.3f seconds\r\n", $end);
unset($list,$result);
print("Concatenating...");
$start = microtime_float();
$result = '';
for ($_ = 0; $_ < ITERATIONS; $_++)
$result .= 'a';
$end = microtime_float() - $start;
printf("%0.3f seconds\r\n", $end);
?>
results in the implode taking longer 99% of the time. e.g.
Starting benchmark, over 10000 iterations:
Imploding...0.007 seconds
Concatenating...0.003 seconds

- 100,477
- 16
- 156
- 200
-
Over two million iterations: (Imploding...1.913 seconds) (Concatenating...1.130 seconds) – Julian Dec 28 '10 at 16:32
-
I think it would be interesting to see what would happen if you did $list[$_] = 'a'; Would that be faster or slower, because if you do $list[] php has to figure out where to put the 'a' string... To bad I don't have a computer here that is running php... – bkwint Sep 14 '12 at 12:38
-
Turns out that even specifing the key as asked here above, so using $list[$_] doesn't change anything regarding the time itself. – ZioCain Jun 18 '21 at 13:10
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.
As of php 7.4.3
Script1.php
$startMemory = memory_get_usage();
$t = microtime(true);
$array = '';
for ($i = 0; $i < 500000; $i++) {
$array .=$i . ',';
}
echo $array . '<br>';
$time = (microtime(true) - $t) . ' Seconds<br>';
$memory = (memory_get_usage() - $startMemory) / 1024 / 1024 . ' MB <br>';
echo $time . $memory;
// 0.11040306091309 Seconds
// 7.9923934936523 MB
Script2.php
$startMemory = memory_get_usage();
$t = microtime(true);
$array = array();
for ($i = 0; $i < 500000; $i++) {
array_push($array, $i);
}
echo implode(",", $array) . '<br>';
$time = (microtime(true) - $t) . ' Seconds<br>';
$memory = (memory_get_usage() - $startMemory) / 1024 / 1024 . ' MB <br>';
echo $time . $memory;
// 0.32534003257751 Seconds
// 21.992469787598 MB
Script3.php
$startMemory = memory_get_usage();
$t = microtime(true);
$array = array();
for ($i = 0; $i < 500000; $i++) {
$array[] = $i;
}
echo implode(",", $array) . '<br>';
$time = (microtime(true) - $t) . ' Seconds<br>';
$memory = (memory_get_usage() - $startMemory) / 1024 / 1024 . ' MB <br>';
echo $time . $memory;
// 0.087002038955688 Seconds
// 21.992446899414 MB
Implode function is double faster than string concatenation. But memory usage is very less for string concatenation

- 1,111
- 1
- 9
- 14