String concatenation performance

Cristian.Codorean cristian.codorean at gmail.com
Thu May 11 15:58:28 CEST 2006


I was just reading a "Python Speed/Performance Tips" article on the
Python wiki
http://wiki.python.org/moin/PythonSpeed/PerformanceTips
and I got to the part that talks about string concatenation and that it
is faster when using join instead of += because of strings being
immutable.  So I have tried it:

from time import time
t=time()

s='almfklasmfkmaskmkmasfkmkqemkmqeqw'
for x in range(40):
    #s+= s[len(s)/2:]
    s="".join((s,s[len(s)/2:]))

print 'duration', time() - t

And I get 1.55016708374 for the concatenation and 3.01116681099 for the
join. I have also tried to put the join outside but it is still a
little bit over 3.
I'm using Python 2.4.2, GCC 3.3.3 (SuSE Linux).

So what am I doing wrong ?




More information about the Python-list mailing list