From:             revealator at myrealbox dot com
Operating system: Windows XP SP2
PHP version:      5.3CVS-2008-09-29 (snap)
PHP Bug Type:     Performance problem
Bug description:  string concatenation really slow

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 bug report at http://bugs.php.net/?id=46198&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=46198&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=46198&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=46198&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=46198&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=46198&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=46198&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=46198&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=46198&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=46198&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=46198&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=46198&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=46198&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=46198&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=46198&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=46198&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=46198&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=46198&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=46198&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=46198&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=46198&r=mysqlcfg

Reply via email to