Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'preferably': 0.03; 'python': 0.08; 'integers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'iterator.': 0.16; 'py3': 0.16; 'set:': 0.16; 'subject:() ': 0.16; 'subject:issues': 0.16; 'written': 0.17; 'simpler': 0.19; '(which': 0.20; 'maybe': 0.22; 'header:In-Reply-To:1': 0.22; 'stuff': 0.22; 'produces': 0.23; 'vs.': 0.23; 'runs': 0.23; 'code': 0.24; 'language.': 0.28; 'version,': 0.28; 'stefan': 0.29; 'version': 0.30; 'least': 0.31; 'list:': 0.31; 'cases': 0.32; 'does': 0.32; 'it.': 0.33; 'to:addr:python-list': 0.34; 'header:X -Complaints-To:1': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'difference': 0.34; 'e.g.': 0.34; 'skip:" 10': 0.36; 'using': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'two': 0.38; 'case': 0.39; 'should': 0.39; 'header:Mime-Version:1': 0.39; 'ways': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'perfect': 0.63; 'due': 0.66; 'here.': 0.66; 'special': 0.67; 'received:188': 0.69; 'moment.': 0.84; 'python2': 0.84; 'numbers:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: range() vs xrange() Python2|3 issues for performance Date: Tue, 02 Aug 2011 11:20:41 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ppp-188-174-26-25.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2 Thunderbird/3.1.11 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312276860 news.xs4all.nl 23953 [2001:888:2000:d::a6]:50536 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10704 harrismh777, 02.08.2011 09:12: > With Python2 you basically have two ways to get a range of numbers: > range() , which returns a list, and > xrange() , which returns an iterator. > > With Python3 you must use range(), which produces an iterator; while > xrange() does not exist at all (at least not on 3.2). That's a good thing. There should be one - and preferably only one - obvious way to do it. iterable: range(N) list: list(range(N)) tuple: tuple(range(N)) set: set(range(N)) ... Less special cases in the language. > This brought up the whole range() xrange() thing for me again because > Python in any case is just not fast enough (no brag, just fact). So my > perfect number stuff is written in C, for the moment. Or use Cython or PyPy, both of which are simpler ways to get your code up to speed. > The interesting thing to note is that > xrange() on Python2 runs "considerably" faster than the same code using > range() on Python3. Are you sure that's due to Py3 range() vs. Py2 xrange()? Py3 has a different implementation for integers (which is still being optimised, BTW). That's much more likely to make a difference here. What version of Py3 were you using? If you used the latest, maybe even the latest hg version, you will notice that that's substantially faster for integers than, e.g. 3.1.x. Stefan