Was going through uses of map
in the code to replace it with imap
(for style mainly) - so I run a performance test on my main use cases:
import random
import string
import timeit
repeat = 3
numbers = 1000
def time(statement, _setup=None):
print min(
timeit.Timer(statement, setup=_setup or setup).repeat(repeat, numbers))
random.seed('slartibartfast')
length = 10
li = [''.join(random.choice(string.ascii_uppercase) for __ in range(16)) for _
in range(length)]
print len(li)
setup = """from itertools import imap
from __main__ import li
"""
print 'join:'
time("','.join(map(str.lower, li))")
time("','.join(imap(str.lower, li))")
print 'tuple:'
time("tuple(map(str.lower, li))")
time("tuple(imap(str.lower, li))")
print 'for:'
time('for _ in (map(str.lower, li)): pass')
time('for _ in (imap(str.lower, li)): pass')
The results for big lists were ok:
10000
join:
4.34010698679
3.95416465252
tuple:
3.57020920625
3.93349155589
for:
3.17242116829
2.66427889191
but the result in the tuple call surprised me - so I run a test with smaller lists that surprised me even more:
10
join:
0.00541496942211
0.0062448383055
tuple:
0.00517576996075
0.00560386370428
for:
0.00521174982402
0.00442818835725
for
still remains faster (that would really be a surprise) but the join and tuple calls are slower when using imap
. The only possible reason I can think of is that they exhaust the iterator - am I right ? but how is then the call faster for join for big lists ?
DISCLAIMER: this is not a performance question, it's about how things are implemented.