Skip to content

Instantly share code, notes, and snippets.

@myouju
Last active October 28, 2015 07:28
Show Gist options
  • Save myouju/04c6ad07732e69272cea to your computer and use it in GitHub Desktop.
Save myouju/04c6ad07732e69272cea to your computer and use it in GitHub Desktop.
compare np.dot to numba.jit
import time
import numba
def matmul1(a, b):
lenI = a.shape[0]
lenJ = a.shape[1]
lenK = b.shape[1]
c = np.zeros((lenI, lenJ))
for i in range(lenI):
for j in range(lenJ):
for k in range(lenK):
c[i, j] += a[i, k] * b[k, j]
return c
@numba.jit
def matmul2(a, b):
lenI = a.shape[0]
lenJ = a.shape[1]
lenK = b.shape[1]
c = np.zeros((lenI, lenJ))
for i in range(lenI):
for k in range(lenK):
for j in range(lenJ):
c[i, j] += a[i, k] * b[k, j]
return c
import numpy as np
a = np.random.randn(1000, 1000)
b = np.random.randn(1000, 1000)
print a.shape
start=time.time()
c2 = matmul2(a, b)
end=time.time()
print '[info] TIME: ' + str(end - start) + ' seconds\n'
#start=time.time()
#c = matmul1(a, b)
#end=time.time()
#print '[info] TIME: ' + str(end - start) + ' seconds\n'
start=time.time()
c3 = np.dot(a, b)
end=time.time()
print '[info] TIME: ' + str(end - start) + ' seconds\n'
$ python a.py
(1000, 1000)
[info] TIME: 1.01345801353 seconds
[info] TIME: 21.63864398 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment