Speed of string += string

Alan McIntyre fusion at thuule.pair.com
Sat Apr 12 14:41:12 EDT 2003


Just off the cuff here, it seems to me that the time required to do 
"string += string" is going to be proportional to the size of the 
original string whether you do it in Python or with the STL's 
std::string or with C strings (so long as you don't know beforehand how 
long the final string will be).

Now I'm inclined to do a comparison just for my own curiosity's 
sake...maybe I'll be back with graphs in just a bit. :)

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?
> 
> TIA,
>    /\/\\ads Orbesen Troest







More information about the Python-list mailing list