Skip to content

Commit 75aa2d9

Browse files
authored
Create decorators_kayra_hokenek.py
1 parent 8cfff6e commit 75aa2d9

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎Week04/decorators_kayra_hokenek.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import functools
2+
import time
3+
import tracemalloc
4+
5+
def performance(fn):
6+
7+
@functools.wraps(fn)
8+
def wrapper(*args, **kwargs):
9+
wrapper.counter += 1
10+
tracemalloc.start()
11+
start = time.perf_counter()
12+
13+
try:
14+
result = fn(*args, **kwargs)
15+
finally:
16+
elapsed = time.perf_counter() - start
17+
current, peak = tracemalloc.get_traced_memory()
18+
tracemalloc.stop()
19+
20+
wrapper.total_time += elapsed
21+
wrapper.total_mem += peak
22+
23+
return result
24+
25+
wrapper.counter = 0
26+
wrapper.total_time = 0.0
27+
wrapper.total_mem = 0
28+
29+
return wrapper

0 commit comments

Comments
 (0)