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


Groups > comp.lang.python > #10704

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

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: range() vs xrange() Python2|3 issues for performance
Date 2011-08-02 11:20 +0200
References <prNZp.31508$js7.12436@newsfe01.iad>
Newsgroups comp.lang.python
Message-ID <mailman.1754.1312276860.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

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