StringType add operation seems every inefficient

Lee, Rick rickylee at americasm01.nt.com
Wed Feb 14 08:30:19 EST 2001


If I do something like this:

s = ''
for i in listOfStrings:
    s = s + i

Once listOfStrings is say 10000 members of 100 bytes, it takes forever
(ie. minutes) for the "for" loop to finish.  This is running on a 700MHz
WinNT machine, Python 2.0

Contrast with:

s = ''
blocks = []
for i in listOfStrings:
    s = s + i
    if len(s) > 4000
        blocks.append(s); s = ''

This takes a blink of the eye to finish, so it is faster than the first
piece of code by between 100 and 1000 times.

Is this relative inefficiency in the string add operation expected?

I eventually have to produce a big long string of approx. 10MB.  What is
the fastest way to do it without having to write C-extensions?

- Rick Lee








More information about the Python-list mailing list