Skip to content

Commit 90c5be4

Browse files
authored
Implement performance tracking decorator
This decorator tracks the performance of functions by measuring execution time and memory usage.
1 parent 71f5b39 commit 90c5be4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Week04/decorators_helin_harman.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
import tracemalloc
3+
from functools import wraps
4+
5+
6+
def performance(func):
7+
@wraps(func)
8+
def wrapper(*args, **kwargs):
9+
performance.counter += 1
10+
11+
start_time = time.perf_counter()
12+
tracemalloc.start()
13+
14+
result = func(*args, **kwargs)
15+
16+
current, peak = tracemalloc.get_traced_memory()
17+
tracemalloc.stop()
18+
end_time = time.perf_counter()
19+
20+
performance.total_time += end_time - start_time
21+
performance.total_mem += peak
22+
23+
return result
24+
25+
return wrapper
26+
27+
28+
# decorator attribute'ları (TESTLER BUNU BEKLİYOR)
29+
performance.counter = 0
30+
performance.total_time = 0.0
31+
performance.total_mem = 0

0 commit comments

Comments
 (0)