7 ms·
This change isn't a matter of Python being slower than a compiled language, it's changing the meaning of the code. The line x = x + y creates a copy of the
by joelgibson 4y ago
This change isn't a matter of Python being slower than a compiled language, it's changing the meaning of the code. The line
x = x + y
creates a copy of the array x, adds y to it, and then sets the variable x to that new array. In contrast, the line
x += y
adds the array y in-place into the array x (and so hopefully no other piece of code is relying on x being immutable). This kind of trade-off occurs in pretty much all programming, for instance you see it whenever big-integer libraries are used in C++ or Rust.