Created
          May 3, 2021 12:21 
        
      - 
      
- 
        Save nschloe/eb3bd2520cdbb1378c14887d56c031a2 to your computer and use it in GitHub Desktop. 
    A.T.conj(): caching vs inline
  
        
  
    
      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 | |
| 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() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
For tridiagonal:
For dense: