Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '3.2': 0.05; 'cpython': 0.05; 'modified': 0.05; 'sys': 0.05; 'sep': 0.07; 'python': 0.08; '(it': 0.09; 'integers': 0.09; 'pulled': 0.09; 'am,': 0.13; 'wrote:': 0.15; '[gcc': 0.16; 'freshly': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'notably': 0.16; 'py3': 0.16; 'runs,': 0.16; 'subject:() ': 0.16; 'subject:issues': 0.16; 'variance': 0.16; 'xrange': 0.16; 'def': 0.16; 'dec': 0.17; 'linux': 0.18; 'aug': 0.19; 'received:209.85.210.174': 0.19; 'received:mail-iy0-f174.google.com': 0.19; 'switched': 0.19; 'slightly': 0.19; 'compared': 0.22; 'maybe': 0.22; 'header:In- Reply-To:1': 0.22; 'feb': 0.23; 'ones.': 0.23; '-----': 0.23; 'tue,': 0.23; 'runs': 0.23; 'windows': 0.26; 'tried': 0.27; "i'm": 0.27; 'version,': 0.28; "wasn't": 0.28; 'bit': 0.28; 'message- id:@mail.gmail.com': 0.28; 'import': 0.29; 'script': 0.29; 'stefan': 0.29; 'environment': 0.29; 'skip:( 20': 0.30; 'fairly': 0.30; 'skip:% 20': 0.30; 'version': 0.30; 'shows': 0.32; 'time:': 0.32; 'to:addr:python-list': 0.34; 'difference': 0.34; 'e.g.': 0.34; 'test': 0.34; "isn't": 0.35; 'core': 0.36; 'doing': 0.37; 'but': 0.37; 'several': 0.37; 'using': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'platform': 0.38; 'correctly': 0.39; 'rest': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; 'did': 0.40; 'results': 0.62; 'overall': 0.63; 'show': 0.67; 'heavy': 0.71; '10:20': 0.84; 'system),': 0.84; 'timings': 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=bYTQRKwbzWVHubArbaQM27ERSDDFV2zPn465mkLX2ws=; b=boE7JAQrdOmkqTD2XXMd315IN0E2MfFDeeyGVvXc68NGwacdBdJIhupFo9YJZcbTQ2 7z215ld7FiyuoVdJ0NOlGLMYiptMHejcaR05irK5/WkJ/R3aWZBJz4EK4euBdLt73G9B vrQ6sKZDffRIsQW0LzdH29DDhOxBO0k+TDB+8= MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 2 Aug 2011 10:52:59 +0100 Subject: Re: range() vs xrange() Python2|3 issues for performance From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312278781 news.xs4all.nl 23939 [2001:888:2000:d::a6]:59569 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10707 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 the same guts: ----- import sys print(sys.version) import time def PNums(q): start=time.clock() for i in range(2, q): m = 1 s = 0 while m <= i/2: if not i%m: s += m m += 1 if i == s: print(i) print("Time: %f"%(time.clock()-start)) return # PNums(33550337) PNums(10000) ----- On my dual-core Windows laptop (it always saturates one core with this, leaving the other for the rest of the system), the results show no statistically significant difference between xrange and range in Python 2, but notably slower overall performance in Python 3: 2.4.5 (#1, Dec 15 2009, 16:41:19) [GCC 4.1.1] Time: 14.474343 Using xrange: 14.415412 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] Time: 8.990142 Using xrange: 9.015566 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] Time: 24.401461 Since I don't have a build environment in which to test the latest from hg, I switched to a Linux box. The following timings therefore cannot be compared with the above ones. 3.3a0 (default:b95096303ed2, Jun 2 2011, 20:43:01) [GCC 4.4.5] Time: 34.390000 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] Time: 13.730000 Using xrange: 13.670000 My 3.3a0 is freshly pulled from hg, although I'm not sure if sys.version has been correctly built. Once again, 2.6.6 shows no significant difference between range and xrange, but 3 is noticeably slower than 2. (I did several runs, but the variance between the runs wasn't significant.) Of course, this is all fairly moot; if you're doing really heavy number crunching, CPython isn't the platform to use. ChrisA