Skip to content

Commit e26c157

Browse files
authored
Fea: recursion and parallel processing optimization (#3)
* Recursion optimization v1 * Second optimization pass * Fixed bad expections * Optimized concurrency, adjusted tests
1 parent e69dff4 commit e26c157

File tree

4 files changed

+257
-151
lines changed

4 files changed

+257
-151
lines changed

examples/examples.py

+36-31
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from tempit import tempit
77

88

9+
@tempit
910
class TempitTestClass:
1011
@tempit
11-
def tempit_basic(self):
12-
time.sleep(0.01)
12+
def tempit_basic(self, a: int = 1, b: int = 2):
13+
return a + b
1314

1415
@tempit(run_times=5)
1516
def tempit_5times(self):
@@ -83,26 +84,16 @@ def args_func(a: int = 1, b: int = 2):
8384
return a + b
8485

8586

86-
@tempit(run_times=5)
87-
def tempit_other_thread(a: int = 1, b: int = 2):
88-
return a + b
89-
90-
91-
@tempit(run_times=5)
92-
def tempit_other_process(a: int = 1, b: int = 2):
93-
return a + b
94-
95-
96-
@tempit(run_times=4, concurrent_execution=True, verbose=True)
87+
@tempit(run_times=4, concurrent_execution=True, verbose=False)
9788
def call_long_process_concurrent(n):
98-
for _ in range(2_000_000):
89+
for _ in range(10_000_000):
9990
pass #
10091
return fib(n)
10192

10293

103-
@tempit(run_times=4, concurrent_execution=False, verbose=True)
94+
@tempit(run_times=4, concurrent_execution=False, verbose=False)
10495
def call_long_process_sequential(n):
105-
for _ in range(2_000_000):
96+
for _ in range(10_000_000):
10697
pass #
10798
return fib(n)
10899

@@ -113,27 +104,36 @@ def fib(n):
113104
return fib(n - 2) + fib(n - 1)
114105

115106

116-
@tempit(run_times=3, concurrent_execution=True, verbose=False, check_for_recursion=True)
107+
@tempit(run_times=1, concurrent_execution=True, verbose=False)
117108
def recursive_func(n):
118109
if n < 2:
119110
return n
120111
return recursive_func(n - 2) + recursive_func(n - 1)
121112

122113

123-
@tempit
124-
def non_recursive_func(n):
125-
return n
114+
@tempit(run_times=1, concurrent_execution=True, verbose=False)
115+
def wrapped_recursive_func(n):
116+
def recursive_func_wr(n):
117+
if n < 2:
118+
return n
119+
return recursive_func_wr(n - 2) + recursive_func_wr(n - 1)
120+
121+
return recursive_func(n)
122+
123+
124+
@tempit(run_times=1, concurrent_execution=True, verbose=False)
125+
def tempit_with_recursive_func(n):
126+
return recursive_func(n)
126127

127128

128129
@tempit(run_times=0, verbose=True)
129130
def main():
130-
131131
test_class = TempitTestClass()
132132

133133
print("---CLASS EXAMPLES---")
134134

135135
print("Once in class basic")
136-
test_class.tempit_basic()
136+
_ = test_class.tempit_basic(1, b=2)
137137
print("Concurrency 5 times in class")
138138
test_class.tempit_5times()
139139
print("Concurrency 5 times in class verbose")
@@ -162,19 +162,22 @@ def main():
162162
future_static_method = executor.submit(test_class.static_method, 1, b=2)
163163
_ = future_static_method.result()
164164

165-
with ProcessPoolExecutor(max_workers=1) as executor:
166-
print("Other process methods")
167-
future_basic_method = executor.submit(test_class.tempit_basic)
168-
future_basic_method.result()
169-
_, _ = future_class_method.result()
170-
future_static_method = executor.submit(test_class.static_method, 1, b=2)
171-
_ = future_static_method.result()
165+
if False: # This crashes at the moment if the class is decorated with @tempit
166+
with ProcessPoolExecutor(max_workers=1) as executor:
167+
print("Other process methods. This part crashes")
168+
future_basic_method = executor.submit(test_class.tempit_basic)
169+
future_basic_method.result()
170+
future_class_method = executor.submit(test_class.class_method, 1, b=2)
171+
_, _ = future_class_method.result()
172+
future_static_method = executor.submit(test_class.static_method, 1, b=2)
173+
_ = future_static_method.result()
172174

173175
print("---END CLASS EXAMPLES---")
174176

175177
print("---FUNCTION EXAMPLES---")
176178
print("Once basic")
177179
tempit_basic()
180+
178181
print("Concurrency 5 times")
179182
tempit_5times()
180183
print("Concurrency 5 times verbose")
@@ -204,9 +207,11 @@ def main():
204207

205208
print("---END FUNCTION EXAMPLES---")
206209
print("---OTHER EXAMPLES---")
207-
_ = call_long_process_concurrent(16)
208-
_ = call_long_process_sequential(16)
209210
_ = recursive_func(10)
211+
_ = wrapped_recursive_func(10)
212+
_ = tempit_with_recursive_func(10)
213+
_ = call_long_process_sequential(16)
214+
_ = call_long_process_concurrent(16)
210215
print("---END OTHER EXAMPLES---")
211216

212217

0 commit comments

Comments
 (0)