PATCH: Speed up direct string concatenation by 20+%!

Fredrik Lundh fredrik at
Mon Oct 2 07:25:53 CEST 2006

Larry Hastings wrote:

> There are some improvements in this version.  Specifically:
> * Python will no longer crash if you do ten million prepends
>   ( x = 'a' + x ).  Since the problem was blowing the stack
>   with an incredibly deep render, I now limit the depth of
>   the string concatenation objects (currently set at 16384).
>   Note that string prepending is now *immensely* faster, as
>   prepending in the existing implementation is a worst-case.

You really should look up something called "ropes".

You should also benchmark this against code that uses the ordinary 
append/join pattern.  (you've posted conflicting benchmarks for 2.5,
but if I'm trusting the benchmarks that looks more reasonable, the
standard implementation pattern is still around 10 times faster than 
your approach).

 > Perhaps; I've never been to PyCon, but it might be fun to give a
 > presentation there.  That said, it would be way more relevant if the
 > patch got accepted, don'tcha think?

It's rather unlikely that something like this will ever be added to
the 2.X series.  It's pretty unlikely for 3.X as well (GvR used a 
rope-like structure for ABC, and it was no fun for anyone), but it'll 
most likely be a lot easier to provide this as an option for 3.X.


More information about the Python-list mailing list