Skip to content

Commit d232033

Browse files
authored
Add performance decorator for function metrics
Implement a performance decorator to measure execution time and memory usage.
1 parent 71f5b39 commit d232033

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Week04/decorators_utku_yuksel.py

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

0 commit comments

Comments
 (0)