Speed of string += string

Daniel Berlin dberlin at dberlin.org
Sat Apr 12 20:57:03 EDT 2003


> Surprisingly, += doesn't appear to be a major time waster for small
> (meaning about 10000 bytes) strings.  I'm guessing it's because the
> malloc my system (Debian Linux) is optimized for small memory
> allocations.

I've got a gnats to bugzilla converter (rewritten from a perl version)
that was taking 15 minutes on a very large (21 meg) pr containing 300k
lines of preprocessed source code in one gnats fields.

The speed was because in my direct conversion, i had transformed .= into
+=

All of the time was spent in a line like:
	fields[hdr] += line

After remember strings are immutable (I've been out of practice with
python for a year or two, since i'm a compiler hacker, not a script
hacker or application developer).
 Appending to a list and joining, or appending to a array('c') takes 4
seconds, instead of the 15 minutes.
:)





More information about the Python-list mailing list