-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnpytest.py
126 lines (109 loc) · 8.82 KB
/
npytest.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import time
# for now this is just the same as list, so it's useless... but maybe it will be expanded in the future
class MatriplNP:
def __init__(self, elem_array):
self.m_ = elem_array
def __getitem__(self, key):
return self.m_[key]
def __setitem__(self, key, value):
self.m_[key] = value
########## first attempt
def Multiply66(A, B, C):
C[ 0] = A[ 0]*B[ 0] + A[ 1]*B[ 6] + A[ 2]*B[12] + A[ 3]*B[18] + A[ 4]*B[24] + A[ 5]*B[30]
C[ 1] = A[ 0]*B[ 1] + A[ 1]*B[ 7] + A[ 2]*B[13] + A[ 3]*B[19] + A[ 4]*B[25] + A[ 5]*B[31]
C[ 2] = A[ 0]*B[ 2] + A[ 1]*B[ 8] + A[ 2]*B[14] + A[ 3]*B[20] + A[ 4]*B[26] + A[ 5]*B[32]
C[ 3] = A[ 0]*B[ 3] + A[ 1]*B[ 9] + A[ 2]*B[15] + A[ 3]*B[21] + A[ 4]*B[27] + A[ 5]*B[33]
C[ 4] = A[ 0]*B[ 4] + A[ 1]*B[10] + A[ 2]*B[16] + A[ 3]*B[22] + A[ 4]*B[28] + A[ 5]*B[34]
C[ 5] = A[ 0]*B[ 5] + A[ 1]*B[11] + A[ 2]*B[17] + A[ 3]*B[23] + A[ 4]*B[29] + A[ 5]*B[35]
C[ 6] = A[ 6]*B[ 0] + A[ 7]*B[ 6] + A[ 8]*B[12] + A[ 9]*B[18] + A[10]*B[24] + A[11]*B[30]
C[ 7] = A[ 6]*B[ 1] + A[ 7]*B[ 7] + A[ 8]*B[13] + A[ 9]*B[19] + A[10]*B[25] + A[11]*B[31]
C[ 8] = A[ 6]*B[ 2] + A[ 7]*B[ 8] + A[ 8]*B[14] + A[ 9]*B[20] + A[10]*B[26] + A[11]*B[32]
C[ 9] = A[ 6]*B[ 3] + A[ 7]*B[ 9] + A[ 8]*B[15] + A[ 9]*B[21] + A[10]*B[27] + A[11]*B[33]
C[10] = A[ 6]*B[ 4] + A[ 7]*B[10] + A[ 8]*B[16] + A[ 9]*B[22] + A[10]*B[28] + A[11]*B[34]
C[11] = A[ 6]*B[ 5] + A[ 7]*B[11] + A[ 8]*B[17] + A[ 9]*B[23] + A[10]*B[29] + A[11]*B[35]
C[12] = A[12]*B[ 0] + A[13]*B[ 6] + A[14]*B[12] + A[15]*B[18] + A[16]*B[24] + A[17]*B[30]
C[13] = A[12]*B[ 1] + A[13]*B[ 7] + A[14]*B[13] + A[15]*B[19] + A[16]*B[25] + A[17]*B[31]
C[14] = A[12]*B[ 2] + A[13]*B[ 8] + A[14]*B[14] + A[15]*B[20] + A[16]*B[26] + A[17]*B[32]
C[15] = A[12]*B[ 3] + A[13]*B[ 9] + A[14]*B[15] + A[15]*B[21] + A[16]*B[27] + A[17]*B[33]
C[16] = A[12]*B[ 4] + A[13]*B[10] + A[14]*B[16] + A[15]*B[22] + A[16]*B[28] + A[17]*B[34]
C[17] = A[12]*B[ 5] + A[13]*B[11] + A[14]*B[17] + A[15]*B[23] + A[16]*B[29] + A[17]*B[35]
C[18] = A[18]*B[ 0] + A[19]*B[ 6] + A[20]*B[12] + A[21]*B[18] + A[22]*B[24] + A[23]*B[30]
C[19] = A[18]*B[ 1] + A[19]*B[ 7] + A[20]*B[13] + A[21]*B[19] + A[22]*B[25] + A[23]*B[31]
C[20] = A[18]*B[ 2] + A[19]*B[ 8] + A[20]*B[14] + A[21]*B[20] + A[22]*B[26] + A[23]*B[32]
C[21] = A[18]*B[ 3] + A[19]*B[ 9] + A[20]*B[15] + A[21]*B[21] + A[22]*B[27] + A[23]*B[33]
C[22] = A[18]*B[ 4] + A[19]*B[10] + A[20]*B[16] + A[21]*B[22] + A[22]*B[28] + A[23]*B[34]
C[23] = A[18]*B[ 5] + A[19]*B[11] + A[20]*B[17] + A[21]*B[23] + A[22]*B[29] + A[23]*B[35]
C[24] = A[24]*B[ 0] + A[25]*B[ 6] + A[26]*B[12] + A[27]*B[18] + A[28]*B[24] + A[29]*B[30]
C[25] = A[24]*B[ 1] + A[25]*B[ 7] + A[26]*B[13] + A[27]*B[19] + A[28]*B[25] + A[29]*B[31]
C[26] = A[24]*B[ 2] + A[25]*B[ 8] + A[26]*B[14] + A[27]*B[20] + A[28]*B[26] + A[29]*B[32]
C[27] = A[24]*B[ 3] + A[25]*B[ 9] + A[26]*B[15] + A[27]*B[21] + A[28]*B[27] + A[29]*B[33]
C[28] = A[24]*B[ 4] + A[25]*B[10] + A[26]*B[16] + A[27]*B[22] + A[28]*B[28] + A[29]*B[34]
C[29] = A[24]*B[ 5] + A[25]*B[11] + A[26]*B[17] + A[27]*B[23] + A[28]*B[29] + A[29]*B[35]
C[30] = A[30]*B[ 0] + A[31]*B[ 6] + A[32]*B[12] + A[33]*B[18] + A[34]*B[24] + A[35]*B[30]
C[31] = A[30]*B[ 1] + A[31]*B[ 7] + A[32]*B[13] + A[33]*B[19] + A[34]*B[25] + A[35]*B[31]
C[32] = A[30]*B[ 2] + A[31]*B[ 8] + A[32]*B[14] + A[33]*B[20] + A[34]*B[26] + A[35]*B[32]
C[33] = A[30]*B[ 3] + A[31]*B[ 9] + A[32]*B[15] + A[33]*B[21] + A[34]*B[27] + A[35]*B[33]
C[34] = A[30]*B[ 4] + A[31]*B[10] + A[32]*B[16] + A[33]*B[22] + A[34]*B[28] + A[35]*B[34]
C[35] = A[30]*B[ 5] + A[31]*B[11] + A[32]*B[17] + A[33]*B[23] + A[34]*B[29] + A[35]*B[35]
NN = 10000000
A = MatriplNP([None] * 36)
for i in range(0,36): A[i] = np.ones(NN,dtype='float')*i
B = MatriplNP([None] * 36)
for i in range(0,36): B[i] = np.ones(NN,dtype='float')*i
C = MatriplNP([None]*36)
t0 = time.time()
Multiply66(A,B,C)
t1 = time.time()
print (t1-t0, (t1-t0)/float(NN))
#for i in range(0,36,6): print A[i][0], A[i+1][0], A[i+2][0], A[i+3][0], A[i+4][0], A[i+5][0]
#for i in range(0,36,6): print B[i][0], B[i+1][0], B[i+2][0], B[i+3][0], B[i+4][0], B[i+5][0]
for i in range(0,36,6): print (C[i][0], C[i+1][0], C[i+2][0], C[i+3][0], C[i+4][0], C[i+5][0])
########## second attempt
a = np.ones((36,NN),dtype='float')
for i in range(0,36): a[i,:] = a[i,:]*i
b = np.ones((36,NN),dtype='float')
for i in range(0,36): b[i,:] = b[i,:]*i
def Multiply_v2(A, B, C):
C[ 0,:] = A[ 0,:]*B[ 0,:] + A[ 1,:]*B[ 6,:] + A[ 2,:]*B[12,:] + A[ 3,:]*B[18,:] + A[ 4,:]*B[24,:] + A[ 5,:]*B[30,:]
C[ 1,:] = A[ 0,:]*B[ 1,:] + A[ 1,:]*B[ 7,:] + A[ 2,:]*B[13,:] + A[ 3,:]*B[19,:] + A[ 4,:]*B[25,:] + A[ 5,:]*B[31,:]
C[ 2,:] = A[ 0,:]*B[ 2,:] + A[ 1,:]*B[ 8,:] + A[ 2,:]*B[14,:] + A[ 3,:]*B[20,:] + A[ 4,:]*B[26,:] + A[ 5,:]*B[32,:]
C[ 3,:] = A[ 0,:]*B[ 3,:] + A[ 1,:]*B[ 9,:] + A[ 2,:]*B[15,:] + A[ 3,:]*B[21,:] + A[ 4,:]*B[27,:] + A[ 5,:]*B[33,:]
C[ 4,:] = A[ 0,:]*B[ 4,:] + A[ 1,:]*B[10,:] + A[ 2,:]*B[16,:] + A[ 3,:]*B[22,:] + A[ 4,:]*B[28,:] + A[ 5,:]*B[34,:]
C[ 5,:] = A[ 0,:]*B[ 5,:] + A[ 1,:]*B[11,:] + A[ 2,:]*B[17,:] + A[ 3,:]*B[23,:] + A[ 4,:]*B[29,:] + A[ 5,:]*B[35,:]
C[ 6,:] = A[ 6,:]*B[ 0,:] + A[ 7,:]*B[ 6,:] + A[ 8,:]*B[12,:] + A[ 9,:]*B[18,:] + A[10,:]*B[24,:] + A[11,:]*B[30,:]
C[ 7,:] = A[ 6,:]*B[ 1,:] + A[ 7,:]*B[ 7,:] + A[ 8,:]*B[13,:] + A[ 9,:]*B[19,:] + A[10,:]*B[25,:] + A[11,:]*B[31,:]
C[ 8,:] = A[ 6,:]*B[ 2,:] + A[ 7,:]*B[ 8,:] + A[ 8,:]*B[14,:] + A[ 9,:]*B[20,:] + A[10,:]*B[26,:] + A[11,:]*B[32,:]
C[ 9,:] = A[ 6,:]*B[ 3,:] + A[ 7,:]*B[ 9,:] + A[ 8,:]*B[15,:] + A[ 9,:]*B[21,:] + A[10,:]*B[27,:] + A[11,:]*B[33,:]
C[10,:] = A[ 6,:]*B[ 4,:] + A[ 7,:]*B[10,:] + A[ 8,:]*B[16,:] + A[ 9,:]*B[22,:] + A[10,:]*B[28,:] + A[11,:]*B[34,:]
C[11,:] = A[ 6,:]*B[ 5,:] + A[ 7,:]*B[11,:] + A[ 8,:]*B[17,:] + A[ 9,:]*B[23,:] + A[10,:]*B[29,:] + A[11,:]*B[35,:]
C[12,:] = A[12,:]*B[ 0,:] + A[13,:]*B[ 6,:] + A[14,:]*B[12,:] + A[15,:]*B[18,:] + A[16,:]*B[24,:] + A[17,:]*B[30,:]
C[13,:] = A[12,:]*B[ 1,:] + A[13,:]*B[ 7,:] + A[14,:]*B[13,:] + A[15,:]*B[19,:] + A[16,:]*B[25,:] + A[17,:]*B[31,:]
C[14,:] = A[12,:]*B[ 2,:] + A[13,:]*B[ 8,:] + A[14,:]*B[14,:] + A[15,:]*B[20,:] + A[16,:]*B[26,:] + A[17,:]*B[32,:]
C[15,:] = A[12,:]*B[ 3,:] + A[13,:]*B[ 9,:] + A[14,:]*B[15,:] + A[15,:]*B[21,:] + A[16,:]*B[27,:] + A[17,:]*B[33,:]
C[16,:] = A[12,:]*B[ 4,:] + A[13,:]*B[10,:] + A[14,:]*B[16,:] + A[15,:]*B[22,:] + A[16,:]*B[28,:] + A[17,:]*B[34,:]
C[17,:] = A[12,:]*B[ 5,:] + A[13,:]*B[11,:] + A[14,:]*B[17,:] + A[15,:]*B[23,:] + A[16,:]*B[29,:] + A[17,:]*B[35,:]
C[18,:] = A[18,:]*B[ 0,:] + A[19,:]*B[ 6,:] + A[20,:]*B[12,:] + A[21,:]*B[18,:] + A[22,:]*B[24,:] + A[23,:]*B[30,:]
C[19,:] = A[18,:]*B[ 1,:] + A[19,:]*B[ 7,:] + A[20,:]*B[13,:] + A[21,:]*B[19,:] + A[22,:]*B[25,:] + A[23,:]*B[31,:]
C[20,:] = A[18,:]*B[ 2,:] + A[19,:]*B[ 8,:] + A[20,:]*B[14,:] + A[21,:]*B[20,:] + A[22,:]*B[26,:] + A[23,:]*B[32,:]
C[21,:] = A[18,:]*B[ 3,:] + A[19,:]*B[ 9,:] + A[20,:]*B[15,:] + A[21,:]*B[21,:] + A[22,:]*B[27,:] + A[23,:]*B[33,:]
C[22,:] = A[18,:]*B[ 4,:] + A[19,:]*B[10,:] + A[20,:]*B[16,:] + A[21,:]*B[22,:] + A[22,:]*B[28,:] + A[23,:]*B[34,:]
C[23,:] = A[18,:]*B[ 5,:] + A[19,:]*B[11,:] + A[20,:]*B[17,:] + A[21,:]*B[23,:] + A[22,:]*B[29,:] + A[23,:]*B[35,:]
C[24,:] = A[24,:]*B[ 0,:] + A[25,:]*B[ 6,:] + A[26,:]*B[12,:] + A[27,:]*B[18,:] + A[28,:]*B[24,:] + A[29,:]*B[30,:]
C[25,:] = A[24,:]*B[ 1,:] + A[25,:]*B[ 7,:] + A[26,:]*B[13,:] + A[27,:]*B[19,:] + A[28,:]*B[25,:] + A[29,:]*B[31,:]
C[26,:] = A[24,:]*B[ 2,:] + A[25,:]*B[ 8,:] + A[26,:]*B[14,:] + A[27,:]*B[20,:] + A[28,:]*B[26,:] + A[29,:]*B[32,:]
C[27,:] = A[24,:]*B[ 3,:] + A[25,:]*B[ 9,:] + A[26,:]*B[15,:] + A[27,:]*B[21,:] + A[28,:]*B[27,:] + A[29,:]*B[33,:]
C[28,:] = A[24,:]*B[ 4,:] + A[25,:]*B[10,:] + A[26,:]*B[16,:] + A[27,:]*B[22,:] + A[28,:]*B[28,:] + A[29,:]*B[34,:]
C[29,:] = A[24,:]*B[ 5,:] + A[25,:]*B[11,:] + A[26,:]*B[17,:] + A[27,:]*B[23,:] + A[28,:]*B[29,:] + A[29,:]*B[35,:]
C[30,:] = A[30,:]*B[ 0,:] + A[31,:]*B[ 6,:] + A[32,:]*B[12,:] + A[33,:]*B[18,:] + A[34,:]*B[24,:] + A[35,:]*B[30,:]
C[31,:] = A[30,:]*B[ 1,:] + A[31,:]*B[ 7,:] + A[32,:]*B[13,:] + A[33,:]*B[19,:] + A[34,:]*B[25,:] + A[35,:]*B[31,:]
C[32,:] = A[30,:]*B[ 2,:] + A[31,:]*B[ 8,:] + A[32,:]*B[14,:] + A[33,:]*B[20,:] + A[34,:]*B[26,:] + A[35,:]*B[32,:]
C[33,:] = A[30,:]*B[ 3,:] + A[31,:]*B[ 9,:] + A[32,:]*B[15,:] + A[33,:]*B[21,:] + A[34,:]*B[27,:] + A[35,:]*B[33,:]
C[34,:] = A[30,:]*B[ 4,:] + A[31,:]*B[10,:] + A[32,:]*B[16,:] + A[33,:]*B[22,:] + A[34,:]*B[28,:] + A[35,:]*B[34,:]
C[35,:] = A[30,:]*B[ 5,:] + A[31,:]*B[11,:] + A[32,:]*B[17,:] + A[33,:]*B[23,:] + A[34,:]*B[29,:] + A[35,:]*B[35,:]
c = np.zeros((36,NN),dtype='float')
t0 = time.time()
Multiply_v2(a,b,c)
t1 = time.time()
print (t1-t0, (t1-t0)/float(NN))
print (c[:,0])