forked from tinyendian/swcmeethpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmulti2.py
30 lines (22 loc) · 828 Bytes
/
matmulti2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
import timeit
def matmul(X,Y):
# use NumPy's "dot" function to multiply matrices
Z = np.dot(X,Y)
# wrapper to make test function callable for timing
def wrapper(func, *args, **kwargs):
def wrapped():
return func(*args, **kwargs)
return wrapped
# set number of rows and columns
N = 1000
# generate square matrices and fill with random numbers
X = np.random.rand(N,N)
Y = np.random.rand(N,N)
# set the number of times the multiplication will be repeated
Ntimes = 10
# run matrix multiplication "Ntimes" times and get runtime minimum
best_runtime = min(timeit.repeat(wrapper(matmul,X,Y), number = 1, repeat = Ntimes))
# output
print "Running NumPy dot function {0} times...".format(Ntimes)
print "Best runtime [seconds]: {0:.4}".format(best_runtime)