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


Groups > comp.lang.python > #10711

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; '3.2': 0.05; 'cpython': 0.05; 'variable,': 0.07; 'python': 0.08; "(i'm": 0.09; '(it': 0.09; 'consistency.': 0.09; 'difference,': 0.09; 'am,': 0.13; 'float': 0.13; 'wrote:': 0.15; 'conclusion:': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'int.': 0.16; 'notably': 0.16; 'seconds,': 0.16; 'subject:() ': 0.16; 'subject:issues': 0.16; 'test).': 0.16; 'worse.': 0.16; 'algorithm': 0.16; 'aug': 0.19; 'int': 0.19; 'received:209.85.210.174': 0.19; 'received:mail- iy0-f174.google.com': 0.19; 'starts': 0.19; 'language': 0.20; 'seconds': 0.21; 'there!': 0.22; 'header:In-Reply-To:1': 0.22; 'feb': 0.23; 'tests.': 0.23; '-----': 0.23; 'tue,': 0.23; 'windows': 0.26; 'changing': 0.28; 'bit': 0.28; 'message- id:@mail.gmail.com': 0.28; 'forgot': 0.29; 'fairly': 0.30; 'closer': 0.30; 'changes': 0.31; 'shows': 0.32; 'references': 0.32; 'time:': 0.32; 'ideal': 0.33; 'to:addr:python-list': 0.34; 'instead': 0.34; 'reasons': 0.34; 'quite': 0.34; 'things': 0.34; '2.6': 0.35; 'integer': 0.35; "isn't": 0.35; 'comparing': 0.37; 'doing': 0.37; 'but': 0.37; 'using': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'somewhat': 0.38; 'two': 0.38; 'should': 0.39; 'manually': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; 'peter': 0.62; 'took': 0.63; 'box,': 0.64; 'heavy': 0.71; 'difference.': 0.84; 'meanwhile,': 0.84; 'otten': 0.84; 'reveals': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=6lG+yxwV/mJfRgAfFDMqhQDFbJUzeifc3VcjHPR0hNs=; b=Oe2jToJ0VVhf7ZFdIa6C3uhWyaH26+O0cyRFw3up0IO99akA5nS84yoZAeDrqR3LfP AdZm/HALpvU1QwlBOUb03of++FDnA28turdTlVSKLM3WTnDhkkqUmDFwqgwZ+S4s9187 TfQDWNLw3xGbNBTzRDr1zs3RvQKbNoaUfE4z8=
MIME-Version 1.0
In-Reply-To <j18ekk$vdn$1@dough.gmane.org>
References <prNZp.31508$js7.12436@newsfe01.iad> <j18ekk$vdn$1@dough.gmane.org>
Date Tue, 2 Aug 2011 11:19:53 +0100
Subject Re: range() vs xrange() Python2|3 issues for performance
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.1757.1312280396.1164.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312280396 news.xs4all.nl 23927 [2001:888:2000:d::a6]:54870
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10711

Show key headers only | View raw


On Tue, Aug 2, 2011 at 10:05 AM, Peter Otten <__peter__@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, 21:29:02) [MSC v.1500 32 bit (Intel)]
Time: 17.917331

That's a lot closer to the 10-14 seconds that Python 2 was doing, but
still somewhat worse. Of course, no surprises that 2.6 is faster than
2.4.

But now, here's a fairly small change that makes a LOT of difference,
and reveals the value of range/xrange. Notice that m is just being a
classic iteration counter (it starts at 1, increments to a top
limit)...

-----
       for m in xrange(1,i//2+1):
           if not i%m:
               s += m
-----

This brings 2.6.5 down to 5.359383 seconds, or 4.703364 with xrange.
(I'm now changing two references from range to xrange.) Meanwhile, 3.2
has come down to 7.096237 seconds, and 2.4.5 to 8.222261.

Comparing the latest 3.3a0 and 2.6.6 on the other box shows that
there's still a difference. Both of them improve with range instead of
manual incrementing, but 2.6.6 takes 6.950000 seconds and 3.3a0 takes
13.830000 (down from 13.730000 and 34.390000 in the previous test).

Conclusion: Python 3 is notably slower comparing floating point and
integer than Python 2 is comparing int and int. No surprises there!
But get everything working with integers, and use range() instead of
manually incrementing a variable, and things come much more even.

But as I said, CPython isn't the ideal language for heavy number
crunching. On the same Windows box, a Pike program using the same
algorithm took only 2.055 seconds. And a C program took 0.328 seconds.
But if you have other reasons for keeping it in Python, do keep it to
integers!

ChrisA

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