Fastest way to apply a function to an iterable

Peter Otten __peter__ at web.de
Wed May 26 17:09:39 EDT 2010


Stefan Behnel wrote:

> Shashank Singh, 26.05.2010 21:48:
>> What is the most efficient way of applying a function to all the elements
>> of an iterable while discarding the
>> result (i.e. operations are done only for side-effects).
>>
>> For example if I want to save all elements in a list of items (and am not
>> interested in what save() returns), the
>> simplest way is:
>>
>> itemlist = [i1, i2, i3....]
>> for item in itemlist: item.save()
>>
>> It might be squeezing too much but is it possible to do it more
>> efficiently by pushing the looping inside the C code
>> and achieve some gains as is done by using map in place of a for loop
>> when the return values need to be saved?
> 
> 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
> 
> 
> But some people may consider this too ugly to actually use in real code.
 
Which doesn't mean it can't get worse:

>>> from sys import maxint
>>> from operator import attrgetter
>>> from itertools import imap, islice
>>> class I:
...     def __init__(self, i):
...             self.i = i
...     def save(self):
...             print "saving", self.i
...
>>> items = [I(i) for i in range(5)]
>>> next(islice(imap(apply, imap(attrgetter("save"), items)), maxint, 
maxint), None)
saving 0
saving 1
saving 2
saving 3
saving 4


See also the following thread:

http://mail.python.org/pipermail/python-list/2010-January/1233307.html

Most real-world applications should spend significantly more time inside the 
save() methods than in the enclosing loop, so I'd expect the effect of this 
kind of optimization on the speed of your program to be negligable.

Peter



More information about the Python-list mailing list