Skip to content

Commit c1ed83f

Browse files
authored
Add performance decorator with memory and time tracking
Implement a performance decorator to measure execution time and memory usage.
1 parent 71f5b39 commit c1ed83f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
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+
import functools
4+
5+
def performance(func):
6+
@functools.wraps(func)
7+
def wrapper(*args, **kwargs):
8+
tracemalloc.start()
9+
start_time = time.perf_counter()
10+
11+
result = func(*args, **kwargs)
12+
13+
current, peak = tracemalloc.get_traced_memory()
14+
tracemalloc.stop()
15+
end_time = time.perf_counter()
16+
17+
performance.counter += 1
18+
performance.total_time += (end_time - start_time)
19+
performance.total_mem += peak
20+
21+
return result
22+
return wrapper
23+
24+
performance.counter = 0
25+
performance.total_time = 0
26+
performance.total_mem = 0

0 commit comments

Comments
 (0)