ID: 46198
Updated by: [EMAIL PROTECTED]
Reported By: revealator at myrealbox dot com
-Status: Open
+Status: Verified
Bug Type: Performance problem
Operating System: Windows XP SP2
PHP Version: 5.3CVS-2008-09-29 (snap)
Previous Comments:
------------------------------------------------------------------------
[2008-10-05 22:15:36] [EMAIL PROTECTED]
> $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-09-29 19:24:04] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46198&edit=1