-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (74 loc) · 1.84 KB
/
main.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
import time
from contextlib import contextmanager
import threading
from memoization.memo_decorator import memo
@contextmanager
def stopwatch(format_str):
"""Print execution time. Argument format_str must have '%f' in it."""
start_time = time.time()
yield
end_time = time.time()
print(format_str % (end_time - start_time))
globalda = 120
@memo
def memo_test(n):
total = 0
for num in range(1, 10000000):
total = total + num
return total
@memo
def memo_test2(n, b, c):
total = globalda
for num in range(1, 10000000):
total = total + num
return total
free = 0
@memo
def f(i):
return free + i
class Caller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stopRequested = False
self.lastError = None
def run(self):
i = 0
while not self.stopRequested:
try:
f(i)
except Exception as e:
self.lastError = e
i += 1
if i > 100:
i = 0
def stop(self):
self.stopRequested = True
@memo
def recursion(n):
if n <= 0:
return 0
return n + recursion(n-1)
if __name__ == '__main__':
with stopwatch('[memo_test] 1st: %f sec'):
result = memo_test(2)
print(result)
with stopwatch('[memo_test] 2nd: %f sec'):
result = memo_test(2)
print(result)
with stopwatch('[memo_test2] 1st: %f sec'):
result = memo_test2(1, 2, 3)
print(result)
# Test multiple threads in one process
callers = [Caller(), Caller(), Caller()]
for t in callers:
t.start()
for i in range(10):
time.sleep(0.1)
free = i
for t in callers:
t.stop()
t.join()
for t in callers:
print(t.lastError)
# Test recursive function
recursion(2)