Reputation: 1609
If I have the choice between
''.join( ['a', 'b'] )
and
''.join( ('a', 'b') )
which one should I use (which one is faster)? Does it matter?
Upvotes: 3
Views: 775
Reputation: 250891
They are almost equivalent and you can always time your code using timeit
module:
In [145]: small_lis,small_tup = ['a','b']*10, ('a','b')*10
In [146]: avg_lis,avg_tup = ['a','b']*1000, ('a','b')*1000
In [147]: huge_lis,huge_tup = ['a','b']*10**6, ('a','b')*10**6
Timing results when number of items are 20:
>>> %timeit ''.join(small_lis)
1000000 loops, best of 3: 987 ns per loop
>>> %timeit ''.join(small_tup)
1000000 loops, best of 3: 1 us per loop
Average size(2000 items):
>>> %timeit ''.join(avg_lis)
10000 loops, best of 3: 71.5 us per loop
>>> %timeit ''.join(avg_tup)
10000 loops, best of 3: 72.8 us per loop
Huge size (2* 10**6 items):
>>> %timeit ''.join(huge_lis)
1 loops, best of 3: 79.9 ms per loop
>>> %timeit ''.join(huge_tup)
1 loops, best of 3: 77.5 ms per loop
Upvotes: 5