I want to calculate the row-wise dot product of two matrices of the same dimension as fast as possible. This is the way I am doing it:
import numpy as np
a = np.array([[1,2,3], [3,4,5]])
b = np.array([[1,2,3], [1,2,3]])
result = np.array([])
for row1, row2 in a, b:
result = np.append(result, np.dot(row1, row2))
print result
and of course the output is:
[ 26. 14.]
Check out numpy.einsum for another method:
Looks like
einsum
is a bit faster thaninner1d
:You'll do better avoiding the
append
, but I can't think of a way to avoid the python loop. A custom Ufunc perhaps? I don't think numpy.vectorize will help you here.EDIT
Based on this answer, it looks like
inner1d
might work if the vectors in your real-world problem are 1D.Straightforward way to do that is:
which avoids the python loop and is faster in cases like:
Played around with this and found
inner1d
the fastest:The plot was created with perfplot (a small project of mine)
I came across this answer and re-verified the results with Numpy 1.14.3 running in Python 3.5. For the most part the answers above hold true on my system, although I found that for very large matrices (see example below), all but one of the methods are so close to one another that the performance difference is meaningless.
For smaller matrices, I found that
einsum
was the fastest by a considerable margin, up to a factor of two in some cases.My large matrix example:
So
einsum
is still the fastest on very large matrices, but by a tiny amount. It appears to be a statistically significant (tiny) amount though!