Skip to content

Commit 32243bf

Browse files
authored
Add performance decorator to track function metrics
This decorator tracks the performance metrics of a function, including execution time and memory usage.
1 parent 2024d08 commit 32243bf

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
import tracemalloc
3+
4+
5+
def performance(func):
6+
def _performance(*args, **kwargs):
7+
performance.counter += 1
8+
tracemalloc.start()
9+
start = time.perf_counter()
10+
result = func(*args, **kwargs)
11+
end = time.perf_counter()
12+
current, peak = tracemalloc.get_traced_memory()
13+
tracemalloc.stop()
14+
performance.total_time += end - start
15+
performance.total_mem += peak
16+
return result
17+
return _performance
18+
19+
20+
performance.counter = 0
21+
performance.total_time = 0
22+
performance.total_mem = 0

0 commit comments

Comments
 (0)