Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10703

Re: range() vs xrange() Python2|3 issues for performance

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.kpn.net!pfeed08.wxs.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'sys': 0.05; 'infinite': 0.07; 'worse': 0.07; 'python': 0.08; '__name__': 0.09; 'integers': 0.09; 'nameerror:': 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; 'substitute': 0.09; 'throw': 0.09; 'exception': 0.12; 'wrote:': 0.15; '"__main__":': 0.16; 'billy': 0.16; 'coded': 0.16; 'explanation': 0.16; 'iterator.': 0.16; 'lambda': 0.16; 'magnitude.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:() ': 0.16; 'subject:issues': 0.16; 'versus': 0.16; 'xrange': 0.16; 'def': 0.16; 'written': 0.17; 'extension': 0.22; 'loop': 0.22; 'maybe': 0.22; 'discussion': 0.22; 'stuff': 0.22; 'produces': 0.23; 'runs': 0.23; 'code': 0.24; 'helpful': 0.26; "i'm": 0.27; 'pass': 0.28; 'wondering': 0.30; 'differences': 0.30; 'from:addr:web.de': 0.30; 'that...': 0.30; 'least': 0.31; 'equivalent': 0.31; 'focus': 0.32; 'does': 0.32; "what's": 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'there': 0.34; 'difference': 0.34; '...': 0.34; 'but,': 0.34; 'causing': 0.34; 'test': 0.34; 'things': 0.34; 'integer': 0.35; 'try:': 0.35; 'skip:" 10': 0.36; 'primary': 0.36; 'some': 0.37; 'doing': 0.37; 'but': 0.37; 'using': 0.37; 'received:org': 0.38; 'user': 0.38; 'subject:: ': 0.38; 'question,': 0.38; 'somewhat': 0.38; 'something': 0.38; 'two': 0.38; 'put': 0.38; 'run': 0.39; 'case': 0.39; 'header:Mime- Version:1': 0.39; 'except': 0.39; 'ways': 0.39; 'either': 0.39; 'list,': 0.39; 'under': 0.39; 'to:addr:python.org': 0.39; 'might': 0.39; 'below': 0.62; 'perfect': 0.63; 'below,': 0.65; 'here': 0.66; '"for': 0.67; 'placed': 0.72; 'future,': 0.76; 'blame.': 0.84; 'crunch': 0.84; 'harrismh777': 0.84; 'moment.': 0.84; 'python2': 0.84; 'python2.7': 0.84; 'sum': 0.89; 'interest,': 0.91; 'numbers:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: range() vs xrange() Python2|3 issues for performance
Date Tue, 02 Aug 2011 11:05:43 +0200
Organization None
References <prNZp.31508$js7.12436@newsfe01.iad>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084aa9c.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1753.1312275941.1164.python-list@python.org> (permalink)
Lines 120
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312275941 news.xs4all.nl 23849 [2001:888:2000:d::a6]:54793
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10703

Show key headers only | View raw


harrismh777 wrote:

> The following is intended as a helpful small extension to the xrange()
> range() discussion brought up this past weekend by Billy Mays...
> 
> 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).
> 
>     I have been doing some research in number theory related to Mersenne
> Primes and perfect numbers (perfects, those integers whose primary
> divisors when summed result in the number, not including the number
> itself)... the first few of those being  6, 28, 496, 8128, 33550336, etc
> 
>     Never mind, but you know... are there an infinite number of them?
> ... and of course, are there any "odd" perfect numbers... well not under
> 10^1500....   I digress, sorry ...
> 
>     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. But, what
> about the differences in performance (supposing we were to stay in
> Python for small numbers) between xrange() vs range() [on Python2]
> versus range() [on Python3]?   I have put my code snips below, with some
> explanation below that...  these will run on either Python2 or
> Python3... except that if you substitute xrange() for range() for
> Python2  they will throw an exception on Python3... doh.

try:
    range = xrange
except NameError:
    pass
> 
> So, here is PyPerfectNumbers.py ----------------------------
> 
> def PNums(q):
>      for i in range(2, q):
>          m = 1
>          s = 0
>          while m <= i/2:

i/2 returns a float in Python 3; you should use i//2 for consistency.

>              if not i%m:
>                  s += m
>              m += 1
>          if i == s:
>               print(i)
>      return 0
> 
> def perf(n):
>      sum = 0
>      for i in range(1, n):
>          if n % i == 0:
>              sum += i
>      return sum == n
> 
> fperf = lambda n: n == sum(i for i in range(1, n) if n % i == 0)
> 
> -----------------/end---------------------------------------
> 
> PNums(8200) will crunch out the perfect numbers below 8200.
> 
> perf(33550336) will test to see if 33550336 is a perfect number
> 
> fperf(33550336) is the lambda equivalent of perf()
> 
> 
>     These are coded with range().  The interesting thing to note is that
> xrange() on Python2 runs "considerably" faster than the same code using
> range() on Python3. For large perfect numbers (above 8128) the
> performance difference for perf() is orders of magnitude. 

Python 3's range() is indeed slower, but not orders of magnitude:

$ python3.2 -m timeit -s"r = range(33550336)" "for i in r: pass"
10 loops, best of 3: 1.88 sec per loop
$ python2.7 -m timeit -s"r = xrange(33550336)" "for i in r: pass"
10 loops, best of 3: 1.62 sec per loop

$ cat tmp.py
try:
    range = xrange
except NameError:
    pass

def fperf(n):
    return n == sum(i for i in range(1, n) if not n % i)

if __name__ == "__main__":
    print(fperf(33550336))
$ time python2.7 tmp.py
True

real    0m6.481s
user    0m6.100s
sys     0m0.000s
$ time python3.2 tmp.py
True

real    0m7.925s
user    0m7.520s
sys     0m0.040s

I don't know what's causing the slowdown, maybe the int/long unification is 
to blame.

> Actually,
> range() on Python2 runs somewhat slower than xrange() on Python2, but
> things are much worse on Python3.
>     This is something I never thought to test before Billy's question,
> because I had already decided to work in C for most of my integer
> stuff... like perfects. But now that it sparked my interest, I'm
> wondering if there might be some focus placed on range() performance in
> Python3 for the future, PEP?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

range() vs xrange()  Python2|3 issues for performance harrismh777 <harmar@member.fsf.org> - 2011-08-02 02:12 -0500
  Re: range() vs xrange()  Python2|3 issues for performance garabik-news-2005-05@kassiopeia.juls.savba.sk - 2011-08-02 07:51 +0000
  Re: range() vs xrange()  Python2|3 issues for performance Peter Otten <__peter__@web.de> - 2011-08-02 11:05 +0200
  Re: range() vs xrange()  Python2|3 issues for performance Stefan Behnel <stefan_ml@behnel.de> - 2011-08-02 11:20 +0200
  Re: range() vs xrange()  Python2|3 issues for performance Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-08-02 11:26 +0200
  Re: range() vs xrange() Python2|3 issues for performance Chris Angelico <rosuav@gmail.com> - 2011-08-02 10:52 +0100
  Re: range() vs xrange() Python2|3 issues for performance Chris Angelico <rosuav@gmail.com> - 2011-08-02 11:19 +0100
  Re: range() vs xrange()  Python2|3 issues for performance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-03 00:45 +1000
    Re: range() vs xrange() Python2|3 issues for performance Chris Angelico <rosuav@gmail.com> - 2011-08-02 17:06 +0100
  Re: range() vs xrange()  Python2|3 issues for performance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-04 13:01 +1000
    Re: range() vs xrange() Python2|3 issues for performance Chris Angelico <rosuav@gmail.com> - 2011-08-04 07:59 +0100
      Re: range() vs xrange() Python2|3 issues for performance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-04 19:56 +1000

csiph-web