-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmult_day1.py
More file actions
108 lines (86 loc) · 2.5 KB
/
matmult_day1.py
File metadata and controls
108 lines (86 loc) · 2.5 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Program to multiply two matrices using nested loops
import random
import time
N = 250
# Method 1
#@profile
def create_x(N):
# NxN matrix
X = []
for i in range(N):
X.append([random.randint(0,100) for r in range(N)])
return X
#@profile
def create_y(N):
# Nx(N+1) matrix
Y = []
for i in range(N):
Y.append([random.randint(0,100) for r in range(N+1)])
return Y
#@profile
def create_r(N):
# result is Nx(N+1)
result = []
for i in range(N):
result.append([0] * (N+1))
return result
#@profile
def method_1(X,Y,result):
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
return result
# Method 2
# Optimization
#@profile
def create_x_o(N):
X = [[random.randint(0, 100) for i in range(N)] for j in range(N)]
return X
#@profile
def create_y_o(N):
Y = [[random.randint(0, 100) for i in range(N + 1)] for j in range(N)]
return Y
#@profile
def method_2(X,Y):
result = [[sum(a * b for a, b in zip(X_row, Y_col)) for Y_col in zip(*Y)] for X_row in X]
return result
# Method 3
# Numpy optimization
#@profile
def method_3(N):
import numpy as np
X = np.random.randint(0, 100, size=(N, N))
Y = np.random.randint(0, 100, size=(N, N + 1))
result = np.matmul(X, Y)
return result
#@profile
def print_result(result):
for r in result:
print(r)
## Method one
start_time = time.time()
X = create_x(N)
Y = create_y(N)
result1 = create_r(N)
result1 = method_1(X,Y,result1)
time1 = time.time() - start_time
## Method two
start_time = time.time()
X_o = create_x_o(N)
Y_o = create_y_o(N)
result2 = method_2(X_o,Y_o)
time2 = time.time() - start_time
## Method three
start_time = time.time()
result3 = method_3(N)
time3 = time.time() - start_time
print((time2/time1)*100)
print((time3/time1)*100)
# For method 1 filling the results matrix takes up largest amount of time. This is to some extent fixed in method 2, where list comprehension is used instead.
# However, with numpy, speed increases a lot and instead of taking ~12-13 seconds in method 1, it now takes about 0.5 seconds.
# Method 2 takes ~50% of the time in Method 1 and Method 3 takes about ~1% of the time in Method 1.
# For memory in euler72.py most memory is taken up at line 52 and same for speed. For matmult.py most time is taken up when filling the result matrix.