Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Why generators take long time? Date: Tue, 19 Jan 2016 10:24:03 +0000 Lines: 17 Message-ID: References: <1e092318-5052-4383-aabe-dd979d72645d@googlegroups.com> <569e0beb$0$1621$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de yhWoQBlUzfsfPwQD+Nx00gvFe9hi92bCgTeS5VWAxOCQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'collections': 0.09; 'subject:Why': 0.09; 'python': 0.10; 'jan': 0.11; 'subsequent': 0.15; '2016': 0.16; 'cc:name:python list': 0.16; 'exhausted': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; '>': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'surely': 0.33; 'received:google.com': 0.35; 'next': 0.35; 'subject:time': 0.35; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209': 0.38; 'skip:x 10': 0.40; 'per': 0.62; '"it': 0.84; '"it': 0.84; 'oscar': 0.84; 'subject:long': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=vKCSxhmfrqWcNwK1vi3QwsUejQ6/lFACi3OKmxIoar8=; b=Be8zz6otfFtAHfoyoT7ETYFRAh7pRo0Acr7ujnj6d1uNJujc6k92ULVRSvJ4GtHDNW sxI9dbEvMDfkttUwvsPaBOlV5P5Ax6domYKSGzGQgCIdNONdzWruTUko8gXG4xmm2SHm Si0821FQYsgstdEO38ngKcQOwcY6BaoIdX9newpu3mRYzNgBZrDaqqjei01lDgxYu5RZ /tjX1qfpWZNuUelW44BX52bhPGtk22KmoVx8aaD1i5MU9Vl2rSEWORQi7RBun/piXMvq XkaOskGcZmgwgFXOhATuZaXZq9xeAbMFiXmEcyvJX3mt0w5XUGPyml8XLQduRTj7vM/H zNVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=vKCSxhmfrqWcNwK1vi3QwsUejQ6/lFACi3OKmxIoar8=; b=hsBh6zGS0/+GxeYuAkUrQ2Q0IOWcPaIcqMEAp7RdzI//VbosjZ4UtdMy3vmcxeCDIt vdcyPiMvn66unlGcBDMfbR1C7Rm9AWXRJE0q0tV55syRJka2un9oxfBkJLeSrPD6wl95 /+2QSt0wFToRgCWg04qzoCCsmSn3H9DtbjajuWYPalhVBF+RVhlSf+/5gKTJ/ShYzIn9 gblm9dh6ZVQHcXz1Q+oyRG6z/1HIrjaGNzgmqrSHUYrIJbE0v+YCKxkC2Mn8PFWFAUXg qmLcwPI73ZC/wBVtKGoioT+kXIBpUUKOI3hbWTWAvJgtjz7WBb7X/RL26GykxkxNzwac AVqA== X-Gm-Message-State: ALoCoQm1veIi2P45yoQQ0eAvbfSZCt7n/+mVG5d1knU64R7KxA/DM25J5W/Tmq7XUuDHkvcTueURh/51Nsc1f41O9SZnzGeG6g== X-Received: by 10.112.135.39 with SMTP id pp7mr8379437lbb.43.1453199044066; Tue, 19 Jan 2016 02:24:04 -0800 (PST) In-Reply-To: <569e0beb$0$1621$c3e8da3$5496439d@news.astraweb.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101899 On 19 Jan 2016 10:16, "Steven D'Aprano" wrote: > > [steve@ando ~]$ python -m timeit -s "from collections import deque" > -s "it = iter([i for i in xrange(1000)])" "deque(it, maxlen=0)" > 1000000 loops, best of 3: 0.913 usec per loop > > > [steve@ando ~]$ python -m timeit -s "from collections import deque" > -s "it = (i for i in xrange(1000))" "deque(it, maxlen=0)" > 1000000 loops, best of 3: 0.965 usec per loop Surely the iterator in the above benchmarks is consumed during the first of 1000000 loops. Subsequent loops are just calling next on an exhausted iterator. -- Oscar