MWB
MWB

Reputation: 12567

Does Numpy allocate temporary arrays in expressions like x += 2 * y?

When evaluating expressions like

x += 2 * y

does Numpy first allocate a new temporary array to hold 2*y, add it to x and then delete it, or can it perform this whole operation in-place?

Upvotes: 5

Views: 867

Answers (1)

user2357112
user2357112

Reputation: 281252

Yup, that makes a temporary array.

If you find yourself needing to mitigate NumPy's love of giant scratch arrays, additional libraries like Numexpr can help quite a bit, but make sure you're attributing performance problems to the right causes. Naive attempts to save allocations usually cause massive slowdowns instead of performance improvement.

Upvotes: 4

Related Questions