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

Emile van Sebille emile at fenx.com
Mon Aug 22 18:35:56 CEST 2011


On 8/22/2011 2:55 AM Richard D. Moores said...
> 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?
>

Naaa.. I just ran it twice -- once per ... _this_ is coincidence.  :)

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Emile>python -m timeit  -r 3 -s "n=0" "n=n+1"
10000000 loops, best of 3: 0.108 usec per loop

C:\Documents and Settings\Emile>python -m timeit  -r 3 -s "n=0" "n += 1"
10000000 loops, best of 3: 0.108 usec per loop

C:\Documents and Settings\Emile>





More information about the Python-list mailing list