Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python:': 0.05; 'tests,': 0.07; 'python': 0.08; '21,': 0.09; 'processor,': 0.09; 'received:209.85.160.174': 0.09; 'received:mail- gy0-f174.google.com': 0.09; 'slowest': 0.09; 'subject:" ': 0.10; '*any*': 0.16; '3.2.1': 0.16; 'addition.': 0.16; 'amd': 0.16; 'beyond,': 0.16; 'expression.': 0.16; 'justify': 0.16; 'measured': 0.16; 'python2.5': 0.16; 'resist': 0.16; 'subject:=': 0.16; 'url:pastebin': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply- To:1': 0.22; 'runs': 0.23; '64-bit': 0.23; 'aug': 0.24; 'code.': 0.26; 'windows': 0.26; 'loop': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; '(so': 0.30; 'at,': 0.30; 'get.': 0.30; 'pasted': 0.30; 'really,': 0.30; 'sun,': 0.30; 'difference': 0.34; "we're": 0.34; 'received:209.85.160': 0.35; 'running': 0.35; 'depend': 0.37; 'fastest': 0.37; 'machine': 0.37; 'using': 0.37; 'but': 0.37; 'think': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; "there's": 0.39; "couldn't": 0.39; 'data': 0.39; 'subject: (': 0.39; "it's": 0.40; 'more': 0.60; 'results': 0.61; 'tiny': 0.64; 'believe': 0.65; 'here': 0.65; 'limit': 0.66; 'making': 0.67; 'subject:+': 0.73; 'claim': 0.76; '"they': 0.84; '2.60': 0.84; '620': 0.84; 'days).': 0.84; 'dick': 0.84; 'noise,': 0.84; 'pcs': 0.84; 'variation': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=yDqrlDMKVYmscSVxMLXLBJjvTfjG5gRwvDSvdjiTw88=; b=VRjntS/9rFUeLGmNySQFD4pxno0NDJYplmpF67NXkAcLdxFAMa7CpYeFtKZt+y26eD UxEhemew2hqcPJIr0bgMr+Z725ZdcAdpQJpsQTNDxZ9ddowATtsUBd7JY8iQXNCghSNG d10J3mDI06xCOHF5vL81xKMmPWzQxr6GQ4LvI= MIME-Version: 1.0 In-Reply-To: <4e51ad16$0$29975$c3e8da3$5496439d@news.astraweb.com> References: <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> <4e51ad16$0$29975$c3e8da3$5496439d@news.astraweb.com> From: "Richard D. Moores" Date: Mon, 22 Aug 2011 02:55:11 -0700 Subject: Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314006934 news.xs4all.nl 23881 [2001:888:2000:d::a6]:54504 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12004 On Sun, Aug 21, 2011 at 18:12, Steven D'Aprano wrote: > But really, we're talking about tiny differences in speed. Such trivial > differences are at, or beyond, the limit of what can realistically be > measured on a noisy PC running multiple processes (pretty much all PCs > these days). Here are three runs of each on my computer: > > > [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n = n+1' > 1000000 loops, best of 3: 0.508 usec per loop > [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n = n+1' > 1000000 loops, best of 3: 0.587 usec per loop > [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n = n+1' > 1000000 loops, best of 3: 0.251 usec per loop > > > [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n += 1' > 1000000 loops, best of 3: 0.226 usec per loop > [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n += 1' > 1000000 loops, best of 3: 0.494 usec per loop > [steve@sylar python]$ python2.5 -m timeit -s 'n=0' 'n += 1' > 1000000 loops, best of 3: 0.53 usec per loop > > Look at the variation between runs! About 130% variation between the fastest > and slowest for each expression. And there's no reason to think that the > fastest results shown is as fast as it can get. The time is dominated by > noise, not the addition. > > > For what it's worth, if I try it with a more recent Python: > > [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n = n+1' > 1000000 loops, best of 3: 0.221 usec per loop > [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n = n+1' > 1000000 loops, best of 3: 0.202 usec per loop > [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n = n+1' > 1000000 loops, best of 3: 0.244 usec per loop > > [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n += 1' > 1000000 loops, best of 3: 0.49 usec per loop > [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n += 1' > 1000000 loops, best of 3: 0.176 usec per loop > [steve@sylar python]$ python3.2 -m timeit -s 'n=0' 'n += 1' > 1000000 loops, best of 3: 0.49 usec per loop > > > I simply do not believe that we can justify making *any* claim about the > relative speeds of n=n+1 and n+=1 other than "they are about the same". Any > result you get, faster or slower, will depend more on chance than on any > real or significant difference in the code. I couldn't resist giving it a try. Using Python 3.2.1 on a 64-bit Windows 7 machine with a 2.60 gigahertz AMD Athlon II X4 620 processor, I did 18 tests, alternating between n=n+1 and n+=1 (so 9 each). The fastest for n+=1 was C:\Windows\System32> python -m timeit -r 3 -s "n=0" "n += 1" 10000000 loops, best of 3: 0.0879 usec per loop The slowest for n+=1 was C:\Windows\System32> python -m timeit -r 3 -s "n=0" "n += 1" 10000000 loops, best of 3: 0.0902 usec per loop The fastest for n = n + 1 was C:\Windows\System32> python -m timeit -r 3 -s "n=0" "n=n+1" 10000000 loops, best of 3: 0.0831 usec per loop The slowest for n = n + 1 was C:\Windows\System32> python -m timeit -r 3 -s "n=0" "n=n+1" 10000000 loops, best of 3: 0.0842 usec per loop Coincidence? All the data are pasted at http://pastebin.com/jArPSe56 Dick Moores