Skip to content

Commit 75b1123

Browse files
authored
Add Week 4 HW
1 parent d6a9a39 commit 75b1123

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎Week04/decorators_sedat_celik‎

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

0 commit comments

Comments
 (0)