-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbench.py
273 lines (207 loc) · 5.94 KB
/
bench.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import sys
import re
from timeit import default_timer as timer
from itertools import repeat
from math import sqrt
from multiprocessing import Pool
#import pandas as pd
#import numpy as np
def measure(data, pattern, num):
regex = re.compile(pattern)
matches = re.findall(regex, data)
return matches
def numb():
np.random.seed(42)
size = 100
A, B = np.random.random((size, size)), np.random.random((size, size))
C, D = np.random.random((size * 128,)), np.random.random((size * 128,))
E = np.random.random((int(size / 2), int(size / 4)))
F = np.random.random((int(size / 2), int(size / 2)))
F = np.dot(F, F.T)
G = np.random.random((int(size / 2), int(size / 2)))
# Matrix multiplication
N = 7
for i in range(N):
np.dot(A, B)
del A, B
# Vector multiplication
N = 1000
for i in range(N):
np.dot(C, D)
del C, D
# Singular Value Decomposition (SVD)
N = 2
for i in range(N):
np.linalg.svd(E, full_matrices = False)
del E
# Cholesky Decomposition
N = 2
for i in range(N):
np.linalg.cholesky(F)
# Eigendecomposition
for i in range(N):
np.linalg.eig(G)
def fib(n):
if n <= 1: return 1
return fib(n - 1) + fib(n - 2)
def extract_Digit(nth):
global tmp1, tmp2, acc, den, num
tmp1 = num * nth
tmp2 = tmp1 + acc
tmp1 = tmp2 // den
return tmp1
def eliminate_Digit(d):
global acc, den, num
acc = acc - den * d
acc = acc * 10
num = num * 10
def next_Term(k):
global acc, den, num
k2=k*2+1
acc = acc + num * 2
acc = acc * k2
den = den * k2
num = num * k
def pi(n=100):
global tmp1, tmp2, acc, den, num
tmp1 = 0
tmp2 = 0
acc = 0
den = 1
num = 1
i=0
k=0
while i<n:
k+=1
next_Term(k)
if num > acc:
continue
d=extract_Digit(3)
if d!=extract_Digit(4):
continue
tt=chr(48+d)
i+=1
eliminate_Digit(d)
def reverse_1(s):
reversed_output = ""
for c in s:
reversed_output = c + reversed_output
return reversed_output
def reverse_recursion(s):
if len(s) == 0:
return s
else:
return reverse_recursion(s[1:]) + s[0]
def reverse_5(s):
return s[::-1]
def strb(data):
for i in range(0, 130):
temp=data.lower()
temp=data.replace("is", "was").replace("1","2")
temp=reverse_1(data[1000:13250])
temp=reverse_recursion(reverse_recursion(data[1000:1800]))
temp=reverse_5(data)
temp='|'.join(data.split(' '))
def mat():
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for x in range(10000):
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]
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
def eval_A(i, j):
ij = i + j
return ij * (ij + 1) // 2 + i + 1
def A_sum(u, i):
return sum(u_j / eval_A(i, j) for j, u_j in enumerate(u))
def At_sum(u, i):
return sum(u_j / eval_A(j, i) for j, u_j in enumerate(u))
def multiply_AtAv(u):
r = range(len(u))
tmp = pool.starmap(
A_sum,
zip(repeat(u), r)
)
return pool.starmap(
At_sum,
zip(repeat(tmp), r)
)
def mp_test(n):
u = [1] * n
for _ in range(10):
v = multiply_AtAv(u)
u = multiply_AtAv(v)
vBv = vv = 0
for ue, ve in zip(u, v):
vBv += ue * ve
vv += ve * ve
result = sqrt(vBv/vv)
if __name__ == '__main__':
print("python-speed v1.3 using python v%d.%d.%d" %(sys.version_info[0],sys.version_info[1],sys.version_info[2]))
with open("test_file") as file:
total=0
data = file.read()
factor=1.0
# start_time = timer()
# for i in range(0,70):
# numb()
# elapsed_time = timer() - start_time
# total+=elapsed_time
# print('np:',str(elapsed_time * 1e3))
# start_time = timer()
# for i in range(0,300):
# df = pd.DataFrame([x.split(';') for x in data.split('\n')])
# df = pd.concat([df for _ in range(5)])
# df=df.drop_duplicates()
# df=df.dropna()
# df=df.groupby(level=0)
# elapsed_time = timer() - start_time
# total+=elapsed_time
# print('df:',str(elapsed_time * 1e3))
start_time = timer()
for i in range(3):
strb(data)
elapsed_time = timer() - start_time
total+=elapsed_time
print('string/mem:', str(round(elapsed_time * 1e3, 2)), 'ms')
start_time = timer()
pi(int(factor)*9000)
mat()
elapsed_time = timer() - start_time
total+=elapsed_time
print('pi calc/math:',str(round(elapsed_time * 1e3, 2)), 'ms')
start_time = timer()
for i in range(0, int(factor*125)):
f=measure(data, r'[\w\.+-]+@[\w\.-]+\.[\w\.-]+', i)
g=measure(data, r'[\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?', i)
h=measure(data, r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])', i)
d=measure(data, '^(?:[^cfdrp].*|.[^a].*|..[^n].*|.{4,}|.{0,2})$',i)
elapsed_time = timer() - start_time
total+=elapsed_time
print('regex:',str(round(elapsed_time * 1e3, 2)), 'ms')
start_time = timer()
fib(int(factor*34))
fib(int(factor*32))
elapsed_time = timer() - start_time
total+=elapsed_time
print('fibonnaci/stack: ',str(round(elapsed_time * 1e3, 2)), 'ms')
start_time = timer()
with Pool(processes=4) as pool:
mp_test(int(factor*432))
elapsed_time = timer() - start_time
total+=elapsed_time
print('multiprocess:', str(round(elapsed_time * 1e3, 2)), 'ms')
print('\ntotal: ', str(round(total * 1e3, 2)), 'ms (lower is better)')