forked from xharaken/step2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_simple.py
More file actions
37 lines (30 loc) · 902 Bytes
/
matrix_simple.py
File metadata and controls
37 lines (30 loc) · 902 Bytes
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
31
32
33
34
35
36
37
import numpy, sys, time
if (len(sys.argv) != 2):
print("usage: python %s N" % sys.argv[0])
quit()
n = int(sys.argv[1])
a = numpy.zeros((n, n)) # Matrix A
b = numpy.zeros((n, n)) # Matrix B
c = numpy.zeros((n, n)) # Matrix C
# Initialize the matrices to some values.
for i in range(n):
for j in range(n):
a[i, j] = i * n + j
b[i, j] = j * n + i
c[i, j] = 0
begin = time.time()
for i in range(n):
for j in range(n):
for k in range(n):
c[i, j] += a[i, k] * b[k, j]
end = time.time()
print("time: %.6f sec" % (end - begin))
# Print C for debugging. Comment out the print before measuring the execution time.
total = 0
for i in range(n):
for j in range(n):
# print c[i, j]
total += c[i, j]
# Print out the sum of all values in C.
# This should be 450 for N=3, 3680 for N=4, and 18250 for N=5.
print("sum: %.6f" % total)