Skip to content

Instantly share code, notes, and snippets.

@nschloe
Last active August 17, 2021 16:51
Show Gist options
  • Save nschloe/ad1d288917a140978f7db6c401cb7f17 to your computer and use it in GitHub Desktop.
Save nschloe/ad1d288917a140978f7db6c401cb7f17 to your computer and use it in GitHub Desktop.
3x3-matvec vs manual matvec
import perfplot
import numpy as np
import npx
A = np.array([[0.0, 1.0, 0.0], [125 / 29, -125 / 29, 0.0], [0.0, 50 / 29, -50 / 29]])
def manual(data):
fx, fy, fz = data
return np.array([fy, 125 / 29 * (fx - fy), 50 / 29 * (fy - fz)])
def at(data):
return A @ data
def np_dot(data):
return np.dot(A, data)
def npx_dot(data):
return npx.dot(A, data)
b = perfplot.bench(
setup=lambda n: np.random.rand(3, n),
kernels=[manual, at, npx_dot, np_dot],
n_range=[2 ** k for k in range(25)],
)
b.save("out.png")
b.show()
@nschloe
Copy link
Author

nschloe commented Aug 17, 2021

The proper matvec is almost always faster:

out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment