Efficient string concatenation methods

Dan Gunter dkgunter at lbl.gov
Sun May 2 13:59:11 EDT 2004


Oliver Crow wrote:
> 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

Thanks for the nice benchmark. I think you should mention one important 
detail though: binary-to-decimal-string conversions like `num` are 
relatively expensive! If you generate these strings ahead of time (e.g. 
put them in a global list), you get a 20%-30% speedup across the board.

def method6a():
         out_str = ''.join([NUMS[num] for num in xrange(loop_count)])
         ps_stats()
         return out_str

loop_count = 500000
NUMS = [`n` for n in xrange(0,loop_count)]
...


-Dan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3415 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20040502/c9601ae3/attachment.bin>


More information about the Python-list mailing list