Skip to content

Commit 72a3977

Browse files
authored
create decorators_rafet_taskin.py
1 parent 71f5b39 commit 72a3977

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Week04/decorators_rafet_taskin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
import tracemalloc
3+
import functools
4+
5+
def performance(func):
6+
@functools.wraps(func) # Preserve metadata of the original function
7+
def wrapper(*args, **kwargs):
8+
tracemalloc.start()
9+
start_time = time.time()
10+
11+
try:
12+
result = func(*args, **kwargs)
13+
finally:
14+
current, peak = tracemalloc.get_traced_memory()
15+
tracemalloc.stop()
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+
return wrapper
24+
25+
performance.counter = 0
26+
performance.total_time = 0
27+
performance.total_mem = 0

0 commit comments

Comments
 (0)