Skip to content

Commit 2437755

Browse files
authored
Week 04 hw
1 parent 9b654ce commit 2437755

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

‎Week04/decorators_ilyas_bugra_filoglu.py‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1+
import functools
12
import time
23
import tracemalloc
3-
from functools import wraps
44

5-
tracemalloc.start()
65

7-
8-
def performance(fn):
9-
@wraps(fn)
6+
def performance(func):
7+
@functools.wraps(func)
108
def wrapper(*args, **kwargs):
11-
12-
performance.counter += 1
13-
9+
is_tracing = tracemalloc.is_tracing()
10+
if not is_tracing:
11+
tracemalloc.start()
1412
tracemalloc.reset_peak()
13+
1514
start_time = time.perf_counter()
16-
result = fn(*args, **kwargs)
17-
18-
end_time = time.perf_counter()
19-
20-
performance.total_time += end_time - start_time
21-
22-
current_memory, peak_memory = tracemalloc.get_traced_memory()
15+
result = func(*args, **kwargs)
16+
elapsed_time = time.perf_counter() - start_time
17+
18+
_, peak_memory = tracemalloc.get_traced_memory()
19+
20+
if not is_tracing:
21+
tracemalloc.stop()
22+
23+
performance.counter += 1
24+
performance.total_time += elapsed_time
2325
performance.total_mem += peak_memory
2426

2527
return result
2628

2729
return wrapper
28-
30+
2931

3032
performance.counter = 0
3133
performance.total_time = 0.0

0 commit comments

Comments
 (0)