Consume an iterable

Muhammad Alkarouri malkarouri at gmail.com
Sat Jan 23 12:23:17 CET 2010


Thanks everyone, but not on my machine (Python 2.6.1, OS X 10.6) it's
not:


In [1]: from itertools import count, islice

In [2]: from collections import deque

In [3]: i1=count()

In [4]: def consume1(iterator, n):
   ...:     deque(islice(iterator, n), maxlen=0)
   ...:
   ...:

In [5]: i2=count()

In [6]: def consume2(iterator, n):
   ...:     for _ in islice(iterator, n): pass
   ...:
   ...:

In [7]: timeit consume1(i1, 10)
1000000 loops, best of 3: 1.63 us per loop

In [8]: timeit consume2(i2, 10)
1000000 loops, best of 3: 846 ns per loop


Can somebody please test whether it is only my machine or is this
reproducible?

(Thanks Jan for making me actually carry the test)



More information about the Python-list mailing list