[New-bugs-announce] [issue7764] Doc for itertools recipe consume is complicated and less efficient

Muhammad Alkarouri report at bugs.python.org
Sat Jan 23 14:53:01 CET 2010


New submission from Muhammad Alkarouri <malkarouri at gmail.com>:

Based on the discussion at:
http://groups.google.co.uk/group/comp.lang.python/browse_thread/thread/c1ae3513a31eb63e/d0701a9902732c67?hl=en#d0701a9902732c67

In the documentation of itertools, one of the functions provided in the recipes section (10.7.3) is consume:

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    collections.deque(islice(iterator, n), maxlen=0)

A clearer implementation is:

def consume(n, items):
    if n == 0:
        return
    next(islice(items, n-1, None), None)

It uses no fancy tricks and is thus more suitable for a documentation. Moreover, the second implementation is actually more efficient. Some timings are provided in the thread linked above. As an example, here are the timings on my machine (Python 2.6.1, x86_64, OS X 10.6:

consume_deque #old implementation
    10: 1.2913839817
   100: 3.18093585968
  1000: 21.6316840649

consume_forloop #using a straight for loop
    10: 0.658184051514
   100: 2.85271406174
  1000: 24.6730420589

consume_islice #the suggested implementation
    10: 0.688861131668
   100: 1.15058612823
  1000: 5.52356886864

The script computing these timings is attached. Thanks to Peter Otten for coming up both with the function and the timing script.

----------
assignee: georg.brandl
components: Documentation
files: consume_timeit.py
messages: 98187
nosy: Muhammad.Alkarouri, georg.brandl
severity: normal
status: open
title: Doc for itertools recipe consume is complicated and less efficient
type: performance
Added file: http://bugs.python.org/file15978/consume_timeit.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7764>
_______________________________________


More information about the New-bugs-announce mailing list