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
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
> 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
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
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
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
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
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.
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
>
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'
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
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
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
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
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
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]:
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
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
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(
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
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
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
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
23 matches
Mail list logo