php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46198 string concatenation really slow
Submitted: 2008-09-29 19:24 UTC Modified: 2008-10-24 08:54 UTC
From: revealator at myrealbox dot com Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.3CVS-2008-09-29 (snap) OS: Windows XP SP2
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: revealator at myrealbox dot com
New email:
PHP Version: OS:

 

 [2008-09-29 19:24 UTC] revealator at myrealbox dot com
Description:
------------
inspired by bug #44069 (Huge memory usage with concatenation using . instead of .=)
string concatenation with something like this is really slow:
$newstring = ($newstring . $string); // slow nearly 6 seconds on my machine



Reproduce code:
---------------
$start_time = microtime(true);

$string = str_repeat('This is a teststring.', 50);

echo "Length: " .strlen($string)."\n";
echo "Memory Before:\n".memory_get_usage(true)."\n";
$newstring = "";

for($i = 1; $i <= 2000; $i++)
{
	// $newstring .= $string; // fast 0.02 seconds
	$newstring = ($newstring . $string); // slow nearly 6 seconds on my machine
}
$end_time = microtime(true);
echo "start_time: $start_time\n";
echo "end_time: $end_time\n";

echo "Memory After:\n".memory_get_usage(true)."\n";
echo "Total Length of String:\n".strlen($newstring)."\n";
echo "\n=====\n";
echo "seconds: " . ($end_time-$start_time) . "\n";
echo "\n";

Expected result:
----------------
Length: 1050
Memory Before:
524288
start_time: 1222714498.4688
end_time: 1222714498.4977
Memory After:
2883584
Total Length of String:
2100000

=====
seconds: 0.028898954391479

Actual result:
--------------
Length: 1050
Memory Before:
524288
start_time: 1222714527.1094
end_time: 1222714532.8964
Memory After:
2883584
Total Length of String:
2100000

=====
seconds: 5.7869839668274

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-05 22:15 UTC] lbarnaud@php.net
> $newstring = ($newstring . $string);

Each time you do this, $newstring must be copied to itself (actually a new $newstring). Whereas in "$newstring .= $string", there are chances that only $string have to be copied at the end of $newstring.

The slowdown may also have something to do with processor caches and alignments, and memcpy() implementation (e.g. replace $string by a longer one having a length of e.g. 2048 bytes, there is mostly no slowdown compared to ".=").
 [2008-10-24 08:54 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 06:01:27 2024 UTC