Skip to content

Commit 03ed348

Browse files
authored
Add performance decorator with memory and time tracking
1 parent f083b29 commit 03ed348

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import functools
2+
import time
3+
import tracemalloc
4+
5+
6+
def performance(fn):
7+
@functools.wraps(fn)
8+
def wrapper(*args, **kwargs):
9+
performance.counter += 1
10+
11+
tracemalloc.start()
12+
start_time = time.perf_counter()
13+
14+
result = fn(*args, **kwargs)
15+
16+
end_time = time.perf_counter()
17+
current, peak = tracemalloc.get_traced_memory()
18+
tracemalloc.stop()
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+
performance.counter = 0
29+
performance.total_time = 0.0
30+
performance.total_mem = 0

0 commit comments

Comments
 (0)