Skip to content

Instantly share code, notes, and snippets.

@sojiadeshina
Created November 8, 2019 06:16
Show Gist options
  • Save sojiadeshina/e19d78077a18b531a5216d03e3b58f97 to your computer and use it in GitHub Desktop.
Save sojiadeshina/e19d78077a18b531a5216d03e3b58f97 to your computer and use it in GitHub Desktop.
import numpy as np
from time import time
size = 4096
N = 20
def timed_matrix_multiply(A, B):
t = time()
for i in range(N):
np.dot(A, B)
elapsed = time() - t
return elapsed
A, B = np.random.uniform(size=(size, size)), np.random.uniform(size=(size, size))
elapsed = timed_matrix_multiply(A, B)
print('NumPy : Dotted two %dx%d matrices in %0.2f s.' % (size, size, elapsed / N))
import mxnet.numpy as np
from mxnet import npx
npx.set_np()
def timed_matrix_multiply(A, B):
t = time()
for i in range(N):
C = np.dot(A, B)
C.wait_to_read()
elapsed = time() - t
return elapsed
A, B = np.random.uniform(size=(size, size)), np.random.uniform(size=(size, size))
elapsed = timed_matrix_multiply(A, B)
print('mxnet.numpy : Dotted two %dx%d matrices in %0.2f s.' % (size, size, elapsed / N))
A, B = np.random.uniform(size=(size, size), ctx=npx.gpu()), np.random.uniform(size=(size, size), ctx=npx.gpu())
elapsed = timed_matrix_multiply(A, B)
print('mxnet.numpy on GPU : Dotted two %dx%d matrices in %0.2f s.' % (size, size, elapsed / N))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment