Fastest way to apply a function to an iterable

Stefan Behnel stefan_ml at behnel.de
Wed May 26 17:26:05 EDT 2010


Shashank Singh, 26.05.2010 23:16:
 > I probably didn't state the problem properly. I was assuming the
 > availability of  a static method that could be passed on to map based
 > solution (or imap for that matter).
 >
 > The question was, if one wants to apply a function on each member of list
 > and discard the return value, is it possible to do it more efficiently than
 > having a for loop in python and applying the function of each of the
 > members?
 >
 > Take this run:
 >
 > from itertools import imap
 > from timeit import Timer
 >
 >
 > def save(x): 2 * x
 >
 > from itertools import imap
 > from timeit import Timer
 >
 >
 > def save(x): 2 * x
 >
 > def f1(): map(save, range(1000)) #simple map
 >
 > def f2():
 >    for _ in imap(save, range(1000)): pass #imap
 >
 > def f3():
 >    for x in range(1000):save(x) #simple iteration
 >
 > t1 = Timer("f1()", "from __main__ import f1")
 > print "f1", t1.timeit(number=1000)
 >
 > t2 = Timer("f2()", "from __main__ import f2")
 > print "f2", t2.timeit(number=1000)
 >
 > t3 = Timer("f3()", "from __main__ import f3")
 > print "f3", t3.timeit(number=1000)
 >
 > The output for one run was:
 >
 > f1 0.393015034034
 > f2 0.476230638252
 > f3 0.376324923978
 >
 > =>  simple for loop performs better than map/imap.

Interesting. So you've found the fastest solution already. If a simple for 
loop is both the most readable and the fastest way to do it, it sounds to 
me like you have solved your problem yourself.


 > Another problem with
 > map/imap is that the memory cost is dependent on the length of the list
 > (because of the intermediate mapped list stored) which is not the case for
 > simple for loop.

... neither is it the case for the imap() solution - but that still proves 
to be slower in your timings.

BTW, assuming that you are using Python 2, you'd better use xrange() in 
benchmarks. The list creation of range() can easily spoil your timings.

Stefan




More information about the Python-list mailing list