Archive

Archive for April 28th, 2009

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

FireStats icon Powered by FireStats