Question
np.add(X, 2*Y, out=X) is so slower than np.add(X, Y, out=X); np.add(X, Y, out=X). Is it an actual practice to use np.add(X, Y, out=X); np.add(X, Y, out=X) in the real world case to gain the performance? I have not done a massive numerical computation in numpy, hence would like to know from those who actually working on problems where such difference can be crutial.
I suppose it is, but any other way to make the statement simple while achieving the performance?
Background
Temporary copy demonstrates the impact of temporary copy.
Problem is that there are many other cases where such copies needs to be created and this impact the performance...
- code is not the same from those in the reference.
import numpy as np
import timeit
setup = """
import numpy as np
X = np.ones(100000, dtype=np.int)
Y = np.ones(100000, dtype=np.int)
"""
iterations = 100000
elepased = timeit.timeit(
stmt="np.add(X, 2*Y, out=X)",
setup=setup,
number=iterations
)
elepased / iterations * 1e6
84.75939844996901
elepased = timeit.timeit(
stmt="np.add(X, Y, out=X); np.add(X, Y, out=X)",
setup=setup,
number=iterations
)
elepased / iterations * 1e6
57.947089899971616
Environment
Running in a jupyter notebook on Ubuntu 20.04LTS, 1CPU 8cores
- numpy 1.19.2
- Python 3.8.5 (default, Sep 4 2020, 07:30:14) [GCC 7.3.0]