On Sun, Aug 21, 2011 at 18:12, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> 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 -- http://mail.python.org/mailman/listinfo/python-list