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

Larry Hastings larry at hastings.org
Tue Oct 3 17:11:29 CEST 2006


John Machin wrote:
> Don't you mean y = x[1] or something like that? y = "".join(x) looks
> like a copy-paste error.

You're right, by gum.  Worse than that, my benchmark wasn't actually
*doing* much of anything there; at the end of the run x was still
length 0.  That was sloppy, and I apologize.

I cannot find a speedy equivalent to the 'xappend = x.append' trick for
string concatenation using +.  'operator.__iadd__' is slower, a labored
attempt using 'x = x("a").__add__' is better but still slower.  So far,
just calling 'x += "a"' is the fastest way, and that's 4.4s, still beat
by the 'xappend = x.append' trick at 3.2s.


> Playing the devil's advocate here: 10M adds each of a single byte
> followed by 1 render doesn't seem very typical. How about [...]
> Some of us have Windows boxes and don't have the necessary MS compiler.
> Is there any chance of someone making a 2.5+patch Windows binary?

I actually developed it on Windows.  Here you go:
    http://larryhastings.com/programming/lch.python.2.5.concat.zip
That contains the new python25.dll and my hacked-up benchmark script.
Just back up your existing python25.dll, then unzip to your Python
directory, and you're ready to rock.

Enjoy,


/larry/




More information about the Python-list mailing list