speedy Python strings?

Roy Smith roy at panix.com
Mon Jan 19 20:02:44 EST 2004


In article <buhtjb$ida$1 at news.rz.uni-karlsruhe.de>,
 Uwe Mayer <merkosh at hadiko.de> wrote:

> since strings are
> immutable the string operations are so slow.

More to the point, and I suspect what you're running into, string 
addition is linear with respect to string length because it has to 
create a new string each time and destroy the old one.  Since each 
addition is linear with string length, a loop which adds strings will 
run in quadratic time.

Possibly you want to look at the cStringIO class.

Another possibility is to buffer things in a list, using append to add 
to the end and pop(0) to pop off the front (a roll-your-own queue).  
Lists do not suffer from the quadratic behavor that string addition does.



More information about the Python-list mailing list