Skip to content

Commit d001ef2

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

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎Week04/decorators_kerimcan_dede.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
import tracemalloc
3+
4+
def performance(func):
5+
6+
def wrapper(*args, **kwargs):
7+
8+
tracemalloc.start()
9+
start = time.perf_counter()
10+
start_mem = tracemalloc.get_traced_memory()[0]
11+
tracemalloc.reset_peak()
12+
13+
try:
14+
result = func(*args, **kwargs)
15+
16+
finally:
17+
end = time.perf_counter()
18+
current, peak = tracemalloc.get_traced_memory()
19+
20+
performance.counter += 1
21+
performance.total_time += end - start
22+
performance.total_mem += peak - start_mem
23+
24+
tracemalloc.stop()
25+
26+
return result
27+
return wrapper
28+
29+
performance.counter = 0
30+
performance.total_time = 0
31+
performance.total_mem = 0

0 commit comments

Comments
 (0)