Instantly share code, notes, and snippets.

# markus-beuckelmann/numpy-benchmark.py

Created April 30, 2017 13:06
Show Gist options
• Save markus-beuckelmann/8bc25531b11158431a5b09a45abd6276 to your computer and use it in GitHub Desktop.
A short Python script to benchmark NumPy and show your BLAS setup
This file contains 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
 #!/usr/bin/env python # -*- coding: UTF-8 -*- # Roughly based on: http://stackoverflow.com/questions/11443302/compiling-numpy-with-openblas-integration from __future__ import print_function import numpy as np from time import time # Let's take the randomness out of random numbers (for reproducibility) np.random.seed(0) size = 4096 A, B = np.random.random((size, size)), np.random.random((size, size)) C, D = np.random.random((size * 128,)), np.random.random((size * 128,)) E = np.random.random((int(size / 2), int(size / 4))) F = np.random.random((int(size / 2), int(size / 2))) F = np.dot(F, F.T) G = np.random.random((int(size / 2), int(size / 2))) # Matrix multiplication N = 20 t = time() for i in range(N): np.dot(A, B) delta = time() - t print('Dotted two %dx%d matrices in %0.2f s.' % (size, size, delta / N)) del A, B # Vector multiplication N = 5000 t = time() for i in range(N): np.dot(C, D) delta = time() - t print('Dotted two vectors of length %d in %0.2f ms.' % (size * 128, 1e3 * delta / N)) del C, D # Singular Value Decomposition (SVD) N = 3 t = time() for i in range(N): np.linalg.svd(E, full_matrices = False) delta = time() - t print("SVD of a %dx%d matrix in %0.2f s." % (size / 2, size / 4, delta / N)) del E # Cholesky Decomposition N = 3 t = time() for i in range(N): np.linalg.cholesky(F) delta = time() - t print("Cholesky decomposition of a %dx%d matrix in %0.2f s." % (size / 2, size / 2, delta / N)) # Eigendecomposition t = time() for i in range(N): np.linalg.eig(G) delta = time() - t print("Eigendecomposition of a %dx%d matrix in %0.2f s." % (size / 2, size / 2, delta / N)) print('') print('This was obtained using the following Numpy configuration:') np.__config__.show()

In other words, in `np.dot(A, B)`, your A and B should be small enough to fit into CPU cache.