Speed of string += string

Carl Banks imbosol-1050170059 at aerojockey.com
Sat Apr 12 19:55:18 CEST 2003


Mads Orbesen Troest wrote:
> Hi;
> 
> Given that strings are immutable, is it exceedingly slow (and memory 
> spiking) to do a lot of "string += string" operations in one's code? IOW, 
> does each and every += evaluation lead to a new concatenated copy of the 
> previous string (now freed for garbage collection) and the new string, so 
> that the longer the string to which stuff is appended is, the longer times 
> each += operation takes?

Yes, I think.  There are good ways to do this, though:

Build a list of strings, then join them together at the end.

Use cStringIO module.


-- 
CARL BANKS




More information about the Python-list mailing list