Last active
August 17, 2021 16:51
-
-
Save nschloe/ad1d288917a140978f7db6c401cb7f17 to your computer and use it in GitHub Desktop.
3x3-matvec vs manual matvec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The proper matvec is almost always faster: