Skip to content

Instantly share code, notes, and snippets.

@sylvchev
Last active January 21, 2016 13:08
Show Gist options
  • Save sylvchev/3f3d9a1d267f0cc0652a to your computer and use it in GitHub Desktop.
Save sylvchev/3f3d9a1d267f0cc0652a to your computer and use it in GitHub Desktop.
Testing numpy computation speed up for numpy.multiply vs numpy.dot in special case: for $V \in R^{n \times n}$ and $U \in R^n$, compare the speed up for numpy.dot(V, numpy.diag(U)) and numpy.multiply(V, U)
import numpy as np
from pyriemann.estimation import Covariances
from timeit import Timer
n, t = 1500, 3000
n_repeat = 100
X = np.random.rand(1, n, t)
U = np.random.rand(n)
cov = Covariances()
V = cov.transform(X)
V = V[0,:,:]
t = Timer("numpy.dot(V, numpy.diag(U))", "import numpy; from __main__ import U, V")
dp = np.array(t.repeat(n_repeat, 1))
print ("dot product for ", n, "x", n, " by diag(", n, ") :", "%.2f+/-%.4f seconds"
% (dp.mean(), dp.std()))
# print ("result is:", np.dot(V, np.diag(U)))
t = Timer("numpy.multiply(V, U)", "import numpy; from __main__ import U, V")
ml = np.array(t.repeat(n_repeat, 1))
print ("multiply for ", n, "x", n, " by ", n, " :", "%.2f+/-%.4f seconds"
% (ml.mean(), ml.std()))
# print ("result is:", np.multiply(V, U))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment