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

2011-08-04 Thread Steven D'Aprano
Chris Angelico wrote: > On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano > wrote: >> a, b = divmod(n, i) >> if b == 0: >> total += a+i >> > > Wouldn't this fail on squares? It happens to give correct results as > far as I've checked; no square up to 10,000 is called perfect, and > there are no pe

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

2011-08-04 Thread Chris Angelico
On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano wrote: >        a, b = divmod(n, i) >        if b == 0: >            total += a+i > Wouldn't this fail on squares? It happens to give correct results as far as I've checked; no square up to 10,000 is called perfect, and there are no perfect squares

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

2011-08-03 Thread Steven D'Aprano
harrismh777 wrote: > def perf(n): > sum = 0 > for i in range(1, n): > if n % i == 0: > sum += i > return sum == n A more effective way to speed that up is with a better algorithm: def isperfect(n): if n < 2: return False total = 1 for i in range(

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

2011-08-02 Thread Chris Angelico
On Tue, Aug 2, 2011 at 3:45 PM, Steven D'Aprano wrote: > (But don't make the mistake of doing what I did, which was to attempt to > produce range(29000) in Python 2. After multiple *hours* of swapping, I > was finally able to kill the Python process and get control of my PC again. > Sigh.) >

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

2011-08-02 Thread Steven D'Aprano
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 r

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

2011-08-02 Thread Chris Angelico
On Tue, Aug 2, 2011 at 10:05 AM, Peter Otten <__pete...@web.de> wrote: > i/2 returns a float in Python 3; you should use i//2 for consistency. > And I forgot to make this change before doing my tests. Redoing the Python 3 ones with // quite drastically changes things! 3.2 (r32:88445, Feb 20 2011,

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

2011-08-02 Thread Chris Angelico
On Tue, Aug 2, 2011 at 10:20 AM, Stefan Behnel wrote: > 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. > I just tried this out, using a slightly modified script but t

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

2011-08-02 Thread Thomas Rachel
Am 02.08.2011 09:12 schrieb harrismh777: 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() , whic

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

2011-08-02 Thread Stefan Behnel
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).

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

2011-08-02 Thread Peter Otten
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 r

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

2011-08-02 Thread garabik-news-2005-05
harrismh777 wrote: 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. if 'xrange' not in dir(__builtins__): xrange = range at the beginning of your program will fix that. --

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

2011-08-02 Thread harrismh777
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 Python