Skip to content

Commit 19ff62b

Browse files
authored
Create decorators_ugur_kurt.py
1 parent f083b29 commit 19ff62b

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎Week04/decorators_ugur_kurt.py‎

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

0 commit comments

Comments
 (0)