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


Groups > comp.lang.python > #12004

Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1")

References <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> <mailman.285.1313955635.27778.python-list@python.org> <mailman.287.1313956392.27778.python-list@python.org> <4e51ad16$0$29975$c3e8da3$5496439d@news.astraweb.com>
From "Richard D. Moores" <rdmoores@gmail.com>
Date 2011-08-22 02:55 -0700
Subject Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1")
Newsgroups comp.lang.python
Message-ID <mailman.305.1314006934.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Aug 21, 2011 at 18:12, Steven D'Aprano
<steve+comp.lang.python@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-21 19:27 +0200
  Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 10:48 -0700
    Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 11:03 -0700
  Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Christian Heimes <lists@cheimes.de> - 2011-08-21 20:24 +0200
    Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Roy Smith <roy@panix.com> - 2011-08-21 14:52 -0400
      Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-22 01:17 +0200
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Chris Angelico <rosuav@gmail.com> - 2011-08-22 00:37 +0100
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Terry Reedy <tjreedy@udel.edu> - 2011-08-21 19:38 -0400
          Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-22 02:00 +0200
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Seebs <usenet-nospam@seebs.net> - 2011-08-22 05:33 +0000
  Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Terry Reedy <tjreedy@udel.edu> - 2011-08-21 15:39 -0400
    Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:53 -0700
      Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:55 -0700
      Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:55 -0700
      Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 11:12 +1000
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") "Richard D. Moores" <rdmoores@gmail.com> - 2011-08-22 02:55 -0700
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Emile van Sebille <emile@fenx.com> - 2011-08-22 09:35 -0700
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Emile van Sebille <emile@fenx.com> - 2011-08-22 10:22 -0700
    Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 13:04 -0700
    Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:53 -0700
      Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-22 01:25 +0200
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Chris Angelico <rosuav@gmail.com> - 2011-08-22 00:41 +0100
          Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 11:16 +1000
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Christian Heimes <lists@cheimes.de> - 2011-08-22 04:04 +0200
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Terry Reedy <tjreedy@udel.edu> - 2011-08-21 22:11 -0400
      Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Stephen Hansen <me+list/python@ixokai.io> - 2011-08-21 19:08 -0700
        Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 14:14 +1000
          Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Stephen Hansen <me+list/python@ixokai.io> - 2011-08-21 21:37 -0700
          Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Stephen Hansen <me+list/python@ixokai.io> - 2011-08-21 21:49 -0700
  Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") casevh <casevh@gmail.com> - 2011-08-21 21:14 -0700

csiph-web