[Tutor] String concatenation too slow

Chris Fuller cfuller084 at thinkingplanet.net
Tue Jul 1 08:00:33 CEST 2008


You could try creating a list of strings, and then using a ''.join(list) to 
concatenate, but you are probably going to be best off using the cStringIO 
module (http://docs.python.org/lib/module-cStringIO.html).  Try timing all 
three and see how they compare when joining lots of strings.  The length of 
the strings probably won't matter as much as the number of them.

Cheers

On Tuesday 01 July 2008 00:12, Shrutarshi Basu wrote:
> I'm working on a program to create Lindenmayer systems. These systems
> depend on heavy string rewriting to form complex patterns.I've been
> using string concatenation to read through the string, and then create
> the new one based on a dictionary lookup. However it becomes very slow
> once the string gets very long (several thousand characters). Part of
> it is undoubtedly due to the fact that the algorithm is quadratic (i'm
> trying to find a better way) but I was wondering if there might be a
> faster alternative to string concatenation. Would appending to a list
> of strings be faster? I'm going to be doing thousands of these
> appends, so even a small boost would be helpful.
> Thanks,
> Basu


More information about the Tutor mailing list