-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance_memoize.py
43 lines (32 loc) · 1.01 KB
/
performance_memoize.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
import timeit
CACHE = dict()
def factorial(n: int) -> int:
if n == 0:
return 1
else:
return n * factorial(n - 1)
def run_factorial(n: int)-> int:
cached = CACHE.get(n)
if cached:
return cached
if n == 0:
return 1
else:
result = n * factorial(n - 1)
CACHE[n] = result
return result
stmt_no_cache = "" \
"factorial(500);" \
"factorial(400);" \
"factorial(450);" \
"factorial(350)"
stmt_cache = "" \
"run_factorial(500);" \
"run_factorial(400);" \
"run_factorial(450);" \
"run_factorial(350)"
duration_no_cache = timeit.timeit(stmt=stmt_no_cache, globals=globals(), number=10000)
duration_cache = timeit.timeit(stmt=stmt_cache, globals=globals(), number=10000)
print(f'Duration cache: {duration_cache}')
print(f'Duration no cache: {duration_no_cache}')
print(f'{round(duration_no_cache/duration_cache)} times faster with cache.')