Skip to content

Commit 5a433d3

Browse files
authored
Add performance tracking decorator
This decorator tracks the performance of a function, measuring execution time and peak memory usage.
1 parent 2024d08 commit 5a433d3

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎Week04/decorators_gulsum_ceylan.py‎

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

0 commit comments

Comments
 (0)