Given the two 1D numpy
arrays a
and b
with
N = 100000
a = np.randn(N)
b = np.randn(N)
Why is there a considerable execution time difference between the following two expressions:
# expression 1
c = a @ a * b @ b
# expression 2
c = (a @ a) * (b @ b)
Using the %timeit
magic of Jupyter Notebook I get the following results:
%timeit a @ a * b @ b
223 µs ± 6.97 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
and
%timeit (a @ a) * (b @ b)
17.4 µs ± 27.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)