Skip to content

Commit 44b31f4

Browse files
authored
Add performance tracking decorator
This decorator tracks the performance of functions by measuring execution time and memory usage.
1 parent 71f5b39 commit 44b31f4

File tree

1 file changed

+29
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)