Re: Consume an iterable

2010-01-24 Thread Peter Otten
Raymond Hettinger wrote: > FWIW, the deque() approach becomes even faster in Py2.7 and Py3.1 > which has a high-speed path for the case where maxlen is zero. > Here's a snippet from Modules/_collectionsmodule.c: > > /* Run an iterator to exhaustion. Shortcut for >the extend/extendleft method

Re: Consume an iterable

2010-01-24 Thread Paul Rubin
Raymond Hettinger writes: > This code consumes an iterator to exhaustion. > It is short, sweet, and hard to beat. I've always used sum(1 for x in iterator) or some such. -- http://mail.python.org/mailman/listinfo/python-list

Re: Consume an iterable

2010-01-24 Thread Raymond Hettinger
>      def consume2(iterator, n):  # the approved proposal (see #7764) >          if n is None: >              collections.deque(iterator, maxlen=0) >          else: >              next(islice(iterator, n, n), None) FWIW, the deque() approach becomes even faster in Py2.7 and Py3.1 which has a high

Re: Consume an iterable

2010-01-24 Thread Jan Kaliszewski
24-01-2010, 18:24:49 Peter Otten <__pete...@web.de> wrote: n=None using n=sys.maxint (consume3) is noticeably faster than 0-maxlen deque (consume1 and consume2): That advantage may not survive the next release: http://svn.python.org/view/python/trunk/Modules/_collectionsmodule.c?r1=68145&r2=7

Re: Consume an iterable

2010-01-24 Thread Peter Otten
Jan Kaliszewski wrote: > But the corret results even more distinctly support my thesis -- that for > n=None using n=sys.maxint (consume3) is noticeably faster than 0-maxlen > deque (consume1 and consume2): That advantage may not survive the next release: http://svn.python.org/view/python/trunk/M

Re: Consume an iterable

2010-01-24 Thread Jan Kaliszewski
Don't the results look suspicious to you? Try measuring with iterator = iter([]) You are obviously right, my brain doesn't work well today :-( But the corret results even more distinctly support my thesis -- that for n=None using n=sys.maxint (consume3) is noticeably faster than 0-maxlen d

Re: Consume an iterable

2010-01-24 Thread Peter Otten
Jan Kaliszewski wrote: > Dnia 23-01-2010 o 15:19:56 Peter Otten <__pete...@web.de> napisał(a): > def consume_islice(n, items): next(islice(items, n, n), None) >> >> One problem: the above function doesn't consume the entire iterator like >> the original example does for n=None. Pass

Re: Consume an iterable

2010-01-24 Thread Jan Kaliszewski
Dnia 23-01-2010 o 15:19:56 Peter Otten <__pete...@web.de> napisał(a): def consume_islice(n, items): next(islice(items, n, n), None) One problem: the above function doesn't consume the entire iterator like the original example does for n=None. Passing sys.maxint instead is not pretty.

Re: Consume an iterable

2010-01-23 Thread Peter Otten
Muhammad Alkarouri wrote: > On 23 Jan, 13:46, Peter Otten <__pete...@web.de> wrote: >> def consume_islice(n, items): >> next(islice(items, n, n), None) > I submitted the bug report before considering this alternative, which > is better. I may add this later in the day, but I have to wait a >

Re: Consume an iterable

2010-01-23 Thread Muhammad Alkarouri
On 23 Jan, 13:46, Peter Otten <__pete...@web.de> wrote: > Peter Otten wrote: > > Duncan Booth wrote: > > >> Peter Otten <__pete...@web.de> wrote: > > >>> With next(islice(...), None) I seem to have found a variant that beats > >>> both  competitors. > > >> It has different behaviour for n==0 but I'

Re: Consume an iterable

2010-01-23 Thread Muhammad Alkarouri
On 23 Jan, 13:32, Peter Otten <__pete...@web.de> wrote: > Muhammad Alkarouri wrote: > > The next function performs much better. It is also much more direct > > for the purposes of consume and much more understandable (at least for > > me) as it doesn't require a specialized data structure which is

Re: Consume an iterable

2010-01-23 Thread Peter Otten
Peter Otten wrote: > Duncan Booth wrote: > >> Peter Otten <__pete...@web.de> wrote: >> >>> With next(islice(...), None) I seem to have found a variant that beats >>> both competitors. >>> >> It has different behaviour for n==0 but I'm sure that's easily fixed. > > "Different behaviour" being

Re: Consume an iterable

2010-01-23 Thread Peter Otten
Muhammad Alkarouri wrote: > The next function performs much better. It is also much more direct > for the purposes of consume and much more understandable (at least for > me) as it doesn't require a specialized data structure which is > subsequently not used as such. > I am thus inclined to report

Re: Consume an iterable

2010-01-23 Thread Peter Otten
Duncan Booth wrote: > Peter Otten <__pete...@web.de> wrote: > >> With next(islice(...), None) I seem to have found a variant that beats >> both competitors. >> > It has different behaviour for n==0 but I'm sure that's easily fixed. "Different behaviour" being a euphemism for broken ;) def con

Re: Consume an iterable

2010-01-23 Thread Peter Otten
Duncan Booth wrote: > Peter Otten <__pete...@web.de> wrote: > >> With next(islice(...), None) I seem to have found a variant that beats >> both competitors. >> > It has different behaviour for n==0 but I'm sure that's easily fixed. "Different behaviour" being a euphemism for broken ;) def con

Re: Consume an iterable

2010-01-23 Thread Muhammad Alkarouri
On 23 Jan, 12:45, Peter Otten <__pete...@web.de> wrote: > Muhammad Alkarouri wrote: > > 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]:

Re: Consume an iterable

2010-01-23 Thread Duncan Booth
Peter Otten <__pete...@web.de> wrote: > With next(islice(...), None) I seem to have found a variant that beats > both competitors. > It has different behaviour for n==0 but I'm sure that's easily fixed. -- http://mail.python.org/mailman/listinfo/python-list

Re: Consume an iterable

2010-01-23 Thread Peter Otten
Muhammad Alkarouri wrote: > 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(iterato

Re: Consume an iterable

2010-01-23 Thread Muhammad Alkarouri
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(

Re: Consume an iterable

2010-01-22 Thread Raymond Hettinger
On Jan 22, 6:13 am, Muhammad Alkarouri wrote: > In the python help for itertools, the following function is provided: > > def consume(iterator, n): >     "Advance the iterator n-steps ahead. If n is none, consume > entirely." >     collections.deque(islice(iterator, n), maxlen=0) > > What is the a

Re: Consume an iterable

2010-01-22 Thread Jan Kaliszewski
In the python help for itertools, the following function is provided: def consume(iterator, n): "Advance the iterator n-steps ahead. If n is none, consume entirely." collections.deque(islice(iterator, n), maxlen=0) What is the advantage of using a collections.deque against, say, the foll

Re: Consume an iterable

2010-01-22 Thread Arnaud Delobelle
Muhammad Alkarouri writes: > In the python help for itertools, the following function is provided: > > def consume(iterator, n): > "Advance the iterator n-steps ahead. If n is none, consume > entirely." > collections.deque(islice(iterator, n), maxlen=0) > > What is the advantage of using

Re: Consume an iterable

2010-01-22 Thread Arnaud Delobelle
Muhammad Alkarouri writes: > In the python help for itertools, the following function is provided: > > def consume(iterator, n): > "Advance the iterator n-steps ahead. If n is none, consume > entirely." > collections.deque(islice(iterator, n), maxlen=0) > > What is the advantage of using

Consume an iterable

2010-01-22 Thread Muhammad Alkarouri
In the python help for itertools, the following function is provided: def consume(iterator, n): "Advance the iterator n-steps ahead. If n is none, consume entirely." collections.deque(islice(iterator, n), maxlen=0) What is the advantage of using a collections.deque against, say, the follo