Comment on Speedup from switch to +=Comments−mhzsh4yBut why is it faster? A non-associative translation to byte code (or however python works)?−lnyan4yFor PyTorch, `+=` is interpreted as an in-place operation−onedognight4yMy guess is that it operates in place with no memory allocations or copying.−actually_a_dog4yNot exactly: >>> def f(x): x += 1 ... >>> def g(x): x = x + 1 ... >>> dis.dis(f) 1 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 >>> dis.dis(g) 1 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
Comments
But why is it faster? A non-associative translation to byte code (or however python works)?
For PyTorch, `+=` is interpreted as an in-place operation
My guess is that it operates in place with no memory allocations or copying.
Not exactly: