Efficient string concatenation methods

Peter Hansen peter at engcorp.com
Sat May 1 22:07:24 EDT 2004


Oliver Crow wrote:

> http://www.skymind.com/~ocrow/python_string/
> 
> I'd be happy to hear if anyone else has done similar tests and if
> there are any other good candidate methods that I missed.

You left out the StringIO module (having done only the cStringIO
version of that).

Note also that, for any of the ones which do method calls,
you can speed up the call by saving a reference to the
bound method in a local variable.  For example, in method 4
you can do "app_list = str_list.append" and then use
"app_list(`num`)" instead of str_list.append(`num`).  This
saves an attribute lookup on each loop iteration.  It's
not "idiomatic" to do so except in (a) cases of optimization
obsession, or (b) benchmarks. ;-)

Interesting and useful results.  Thanks! :-)

-Peter



More information about the Python-list mailing list