[Python-ideas] An exhaust() function for iterators
Raymond Hettinger
raymond.hettinger at gmail.com
Sat Oct 12 19:34:26 CEST 2013
On Sep 28, 2013, at 9:06 PM, Clay Sweetser <clay.sweetser at gmail.com> wrote:
>
> As it turns out, the fastest and most efficient method available in
> the standard library is collections.deque's __init__ and extend
> methods.
That technique is shown in the itertools docs in the consume() recipe.
It is the fastest way in CPython (in PyPy, a straight for-loop will
likely be the fastest).
I didn't immortalize it as a real itertool because I think most code is
better-off with a straight for-loop.
The itertools were inspired by functional languages and intended
to be used in a functional style where iterators with side-effects
would be considered bad form.
A regular for-loop is only a little bit slower, but it has a number of virtues
including clarity, signal checking, and thread switching.
In a real application, the speed difference of consume() vs a for-loop
is likely to be insignificant if the iterator is doing anything interesting at all.
Raymond
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131012/2620c31f/attachment-0001.html>
More information about the Python-ideas
mailing list