Why is a.dot(b) faster than a@b although Numpy recommends a@b
Your premise is incorrect. You should use larger matrices to measure performance to avoid function calls dwarfing insignificant calculations.
Using Python 3.60 / NumPy 1.11.3 you will find, as explained here, that @
calls np.matmul
and both outperform np.dot
.
import numpy as np
n = 500
a = np.arange(n**2).reshape(n, n)
b = np.arange(n**2).reshape(n, n)
%timeit a.dot(b) # 134 ms per loop
%timeit a @ b # 71 ms per loop
%timeit np.matmul(a,b) # 70.6 ms per loop
Also note, as explained in the docs, np.dot
is functionally different to @
/ np.matmul
. In particular, they differ in treatment of matrices with dimensions greater than 2.