Fastest way to apply a function to an iterable
Shashank Singh
shashank.sunny.singh at gmail.com
Wed May 26 17:16:20 EDT 2010
On Thu, May 27, 2010 at 1:56 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> If all items have the exact same type, you can get away with an unbound
> method:
>
> class MyType(object):
> ...
> def safe(self):
> ...
>
> itemlist = [ MyType() for i in range(20) ]
>
> # this is what you want:
>
from itertools import imap
> for _ in imap(MyType.save, itemlist): pass
>
Thanks Stefan,
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. 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.
I hope I have explained it better this time.
--
Regards
Shashank Singh
Senior Undergraduate, Department of Computer Science and Engineering
Indian Institute of Technology Bombay
shashank.sunny.singh at gmail.com
http://www.cse.iitb.ac.in/~shashanksingh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100527/0e29e89e/attachment-0001.html>
More information about the Python-list
mailing list