Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created May 3, 2021 12:21
Show Gist options
  • Save nschloe/eb3bd2520cdbb1378c14887d56c031a2 to your computer and use it in GitHub Desktop.
Save nschloe/eb3bd2520cdbb1378c14887d56c031a2 to your computer and use it in GitHub Desktop.
A.T.conj(): caching vs inline
import perfplot
import numpy as np
from scipy.sparse import spdiags
def setup_dense(n):
A = np.random.rand(n, n) + 1j * np.random.rand(n, n)
AH = A.T.conj()
x = np.random.rand(n) + 1j * np.random.rand(n)
return A, AH, x
def setup_tridiag(n):
A = spdiags(np.random.rand(3, n), [-1, 0, 1], n, n)
AH = A.T.conj()
x = np.random.rand(n) + 1j * np.random.rand(n)
return A, AH, x
def AHx(Ax):
A, _, x = Ax
return A.T.conj() @ x
def ATx(Ax):
A, _, x = Ax
return (A.T @ x.conj()).conj()
def cached(Ax):
_, AH, x = Ax
return AH @ x
b = perfplot.bench(
setup=setup_tridiag, kernels=[AHx, ATx, cached], n_range=[2 ** k for k in range(24)]
)
b.save("tridiag.png")
b.show()
@nschloe
Copy link
Author

nschloe commented May 3, 2021

For tridiagonal:

tridiag

For dense:

dense

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