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

Irmen de Jong irmen at -NOSPAM-xs4all.nl
Sun Aug 21 19:14:19 CEST 2011


On 21-08-11 19:03, Laurent wrote:
> Well I agree with you about string concatenation, but here I'm talking about integers incrementation...

Seems the two forms are not 100% identical:

 >>> import dis
 >>> def f1(x):
...  x=x+1
...
 >>> def f2(x):
...  x+=1
...
 >>>
 >>> dis.dis(f1)
   2           0 LOAD_FAST                0 (x)
               3 LOAD_CONST               1 (1)
               6 BINARY_ADD
               7 STORE_FAST               0 (x)
              10 LOAD_CONST               0 (None)
              13 RETURN_VALUE
 >>> dis.dis(f2)
   2           0 LOAD_FAST                0 (x)
               3 LOAD_CONST               1 (1)
               6 INPLACE_ADD
               7 STORE_FAST               0 (x)
              10 LOAD_CONST               0 (None)
              13 RETURN_VALUE
 >>>


What the precise difference (semantics and speed) is between the 
BINARY_ADD and INPLACE_ADD opcodes, I dunno. Look in the Python source 
code or maybe someone knows it from memory :-)

Irmen




More information about the Python-list mailing list