Skip to content

Instantly share code, notes, and snippets.

@anilsathyan7
Created July 31, 2017 15:09
Show Gist options
  • Select an option

  • Save anilsathyan7/0f9e3682132471790738f258021e4524 to your computer and use it in GitHub Desktop.

Select an option

Save anilsathyan7/0f9e3682132471790738f258021e4524 to your computer and use it in GitHub Desktop.
Python numba matrix multiplication
import time
import numba
from numba import jit
import numpy as np
#input matrices
matrix1 = np.random.rand(30,30)
matrix2 = np.random.rand(30,30)
rmatrix = np.zeros(shape=(30,30))
#multiplication function
@jit('void(float64[:,:],float64[:,:],float64[:,:])')
def matmul(matrix1,matrix2,rmatrix):
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
rmatrix[i][j] += matrix1[i][k] * matrix2[k][j]
#Calculate running time                                                                                                             start=time.clock()
matmul(matrix1,matrix2,rmatrix)
end=time.clock()
#print results
print end-start
for r in rmatrix:
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment