Skip to content

Commit 3093c16

Browse files
authored
Add performance tracking decorator
1 parent 71f5b39 commit 3093c16

File tree

1 file changed

+30
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)