Skip to content

Commit 479626d

Browse files
Create decorators_hatice_akgul.py
1 parent 71f5b39 commit 479626d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Week04/decorators_hatice_akgul.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import time
2+
import tracemalloc
3+
import functools
4+
5+
def performance(func):
6+
"""
7+
Fonksiyonun performansını ölçen ve istatistiklerini saklayan dekoratör.
8+
"""
9+
if not hasattr(performance, "counter"):
10+
performance.counter = 0
11+
performance.total_time = 0.0
12+
performance.total_mem = 0
13+
14+
@functools.wraps(func)
15+
def wrapper(*args, **kwargs):
16+
tracemalloc.start()
17+
start_time = time.perf_counter()
18+
result = func(*args, **kwargs)
19+
end_time = time.perf_counter()
20+
21+
current_mem, peak_mem = tracemalloc.get_traced_memory()
22+
tracemalloc.stop()
23+
24+
performance.counter += 1
25+
performance.total_time += (end_time - start_time)
26+
performance.total_mem += peak_mem
27+
28+
return result
29+
30+
return wrapper
31+
32+
performance.counter = 0
33+
performance.total_time = 0.0
34+
performance.total_mem = 0

0 commit comments

Comments
 (0)