Skip to content

Commit 4661a00

Browse files
authored
Implement performance decorator for function metrics
Added a performance decorator to measure execution time and memory usage.
1 parent f083b29 commit 4661a00

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

‎Week04/decorators_melda_gungor.py‎

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

0 commit comments

Comments
 (0)