Speed of string += string

Alex Martelli aleax at aleax.it
Sun Apr 13 05:21:42 EDT 2003


achrist at easystreet.com wrote:

> We went around on this one about 4 years ago. Is this a url:
> 
> 
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=37054490.E79ADCC9%40easystreet.com&rnum=1&prev=/groups%3Fnum%3D100%26hl%3Den%26lr%3D%26ie%3DISO-8859-1%26as_drrb%3Db%26q%3Dgroup%253Acomp.lang.python%2Binsubject%253Astring%2Bauthor%253Aachrist%2540easystreet.com%26btnG%3DGoogle%2BSearch%26as_mind%3D12%26as_minm%3D5%26as_miny%3D1993%26as_maxd%3D12%26as_maxm%3D4%26as_maxy%3D1999

> About 6 messages into the thread, Tim Peters came up with a way that
> was about 5 times faster than string.join().

Not exactly -- the "way" you mention just spreads the costs out,
so they don't SHOW when you're "joining" the pieces (which are
already "joined").  In modern terms:

[alex at lancelot python2.3]$ python -O timeit.py -s'''
import array; SAR=array.array("c")
data=map(str,range(9999))
''' 'for x in data:' ' SAR.fromstring(x)' 'z=SAR.tostring()'
100 loops, best of 3: 2.67e+04 usec per loop

[alex at lancelot python2.3]$ python -O timeit.py -s'''
data=map(str,range(9999))
''' 'z="".join(data)'
100 loops, best of 3: 2.14e+03 usec per loop


Sure, the cost of the .tostring is tiny, but when you have a
lot of little pieces you must loop over them and .fromstring
each -- and THAT is where you're paying the cost.  So .join
is actually an order of magnitude faster...!-)


Alex





More information about the Python-list mailing list