PDA

View Full Version : Fastest string concatenation methods



Kyohack
04-10-2013, 08:14 PM
In a search to further optimize the speed of my PHP scripts, I wanted to determine which method of string concatenation performs fastest. I tested the speed of three common ways of performing string concatenation, and I have documented my findings below.



Using variables directly:
It might not be obvious, but the fastest way to concatenate a string is to not concatenate it. When possible, use your variables directly and allow PHP to concatenate them automatically at run-time. Take this code as an example:


$text = 'text';
$string = "$text$text$text$text$text$text$text$text$text$text";


In the example above, the $text variable is directly used because it is within double quotations. From a theoretical point of view, the concatenation occurs at run-time when the PHP script outputs its contents to the browser. This is the fastest method of string concatenation, and only takes 34.3 seconds when looped 50 million times.



Using the .= operator:
The second fastest method of string concatenation involves using the .= operator. See the example below, in which the $text variable is appended to the end of the $string variable:


$text = 'text';
$string = $text;$string .= $text;$string .= $text;$string .= $text;$string .= $text;$string .= $text;$string .= $text;$string .= $text;$string .= $text;$string .= $text;

Like the first example, the operation is run 10 times. However, this example takes 41.8 seconds to execute when it is looped 50 million times.



Using . to concatenate:
The most common method of string concatenation (and sadly, the slowest method), uses . in between variables or strings. If you're unfamiliar, take a look at this example:


$text = 'text';
$string = $text . $text . $text . $text . $text . $text . $text . $text . $text . $text;


When looped 50 million times, this code takes 45.6 seconds to execute. This is, by far, the slowest way of concatenating a variable. Why? Well, since the . operator is used multiple times, each variable is concatenated separately. So, instead of allowing PHP to concatenate the variables automatically all at once as in the first example, concatenation occurs multiple times.



If you'd like to perform these tests on your own system, here is the code that I used:


<?php
//
// Description: Concatenation speed test.
// Contributor(s): Kyohack
// Last revision: 4/9/2013
//
// Copyright ©2007-2013 PokéCheats. All Rights Reserved.
//

// Bypass the default execution time limit of 30 seconds.
set_time_limit(0);

// Define variable that will be concatenated.
$text = 'text';

// Start timer for first test.
$timeStart = microtime(true);

// Loop the concatenation operation 50 million times.
for ($i = 1; $i < 50000000; $i++) {
$string = $text . $text . $text . $text . $text . $text . $text . $text . $text . $text;
}

// Display the duration of time the operation occupied.
echo 'Took ' . (microtime(true) - $timeStart) . ' seconds.<br />'; // Output: Took 45.649155139923 seconds.


// Start timer for second test.
$timeStart = microtime(true);

// Loop the concatenation operation 50 million times.
for ($i = 1; $i < 50000000; $i++) {
$string = $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
$string .= $text;
}

// Display the duration of time the operation occupied.
echo 'Took ' . (microtime(true) - $timeStart) . ' seconds.<br />'; // Output: Took 44.546118974686 seconds.


// Start timer for third test.
$timeStart = microtime(true);

// Loop the concatenation operation 50 million times.
for ($i = 1; $i < 50000000; $i++) {
$string = "$text$text$text$text$text$text$text$text$text$text";
}

// Display the duration of time the operation occupied.
echo 'Took ' . (microtime(true) - $timeStart) . ' seconds.<br />'; // Output: Took 34.339332103729 seconds.
?>