Python speed-up

Raymond Hettinger vze4rx4y at verizon.net
Thu Sep 23 02:12:12 EDT 2004


[Skip Montanaro]
> >  in 2.4 common case string concatenation gets a big speed boost:
> >
> >     % python2.3 ~/local/bin/timeit.py  -s 's=""' "s+='foo'"
> >     10000 loops, best of 3: 68.3 usec per loop
> >     % python2.4 ~/local/bin/timeit.py  -s 's=""' "s+='foo'"
> >     1000000 loops, best of 3: 0.855 usec per loop

[Gerrit]
> Ah. Am I correct to conclude that StringIO should either be rewritten
> concatenating strings, or be deprecated, or both?

Nope.  StringIO correctly makes heavy use of the ''.join() idiom which is still
preferred += for reasons of portability and speed.  The purpose of the +=
optimization was to make it less catastrophic in places where it does occur.
Guido was clear that ''.join() remains king.

I will submit a patch to tighten up the code in StringIO.write() which needs
localized variables and elimination of an unnecessary step.


Raymond Hettinger





More information about the Python-list mailing list