Skip to content

Commit 1a0d670

Browse files
Solved the decorators homework without using functools module.
1 parent 81c5be5 commit 1a0d670

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import time
22
import tracemalloc
3-
from functools import wraps
43

54
def performance(func):
6-
@wraps(func)
5+
76
def wrapper(*args, **kwargs):
8-
performance.counter += 1
97

108
tracemalloc.start()
11-
start_time = time.perf_counter()
12-
13-
result = func(*args, **kwargs)
14-
15-
end_time = time.perf_counter()
16-
current, peak = tracemalloc.get_traced_memory()
17-
tracemalloc.stop()
18-
19-
performance.total_time += (end_time - start_time)
20-
performance.total_mem += peak
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)
2115

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+
2226
return result
23-
2427
return wrapper
2528

2629
performance.counter = 0
27-
performance.total_time = 0.0
30+
performance.total_time = 0
2831
performance.total_mem = 0

0 commit comments

Comments
 (0)