Home > Developer, Python > Performance of str+str and ‘‘.join()

Performance of str+str and ‘‘.join()

April 28th, 2009
>>> a = 'a' * 100000
>>> def t(a):
>>>     b = ''
>>>     for i in a:
>>>         b = b + '|' + i
>>>     return b
>>> 
>>> time c = t(a)
>>> time c = '|'.join(a)

In my notebook ( Intel(R) Core(TM)2 CPU T5200 @ 1.60GHz, 1.5G Mem):

The result of time c=t(a):
CPU times: user 2.90 s, sys: 0.00 s, total: 2.90 s
Wall time: 3.07 s

The result of time c = ‘|’.join(a):
CPU times: user 0.01 s, sys: 0.00 s, total: 0.01 s
Wall time: 0.01 s

If I changed a from 100000 to 1000000, c=t(a) can’t give me a result in more than 5 minutes while the other one give me the result in 0.09s.

join is much faster than str + str. str + str need to create new strings all the time, but join doesn’t. I guess “join” maybe do something on the memory level.

Developer, Python

  1. Chaobin
    April 14th, 2010 at 16:59 | #1

    Tested on my iMac, join() indeed is faster!
    Guess the point is right referring to the memory usage. Since operator ‘+’ applied on a str will of course force the memory block to be copied in every loop, it deals things slower with that big overhead.

  1. No trackbacks yet.
FireStats icon Powered by FireStats