Efficient string concatenation methods

Oliver Crow ocrow at skymind.com
Sat May 1 20:43:12 EDT 2004


As a realtive python newb, but an old hack in general, I've been
interested in the impact of having string objects (and other
primitives) be immutable.  It seems to me that string concatenation is
a rather common operation, and in Python having immutable strings
results in a performance gotcha for anyone not aware of the impact of
doing lots of concatenation in the obvious way.

I found several sources with advice for how to do concatenation in a
pythonic way (e.g. ref#1), but I hadn't seen any measurements or
comparisons. So, I put together a little test case and ran it through
for six different methods.  Here's the results, and my conclusions:

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.

ref #1: http://manatee.mojam.com/~skip/python/fastpython.html#stringcat

Oliver



More information about the Python-list mailing list