Skip to content

Commit 3feefad

Browse files
Update decorators_fevzi_bagriacik.py
1 parent e5b5a29 commit 3feefad

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import time
22
import tracemalloc
3-
from functools import wraps
3+
from functools import update_wrapper
44

55

6-
def performance(func):
6+
class performance:
77
"""
88
A decorator which measures the performance of functions and
99
also saves some statistics.
1010
"""
1111

12-
@wraps(func)
13-
def wrapper(*args, **kwargs):
12+
def __init__(self, func):
13+
self.func = func
14+
self.counter = 0
15+
self.total_time = 0.0
16+
self.total_mem = 0
17+
update_wrapper(self, func)
18+
19+
def __call__(self, *args, **kwargs):
1420
tracemalloc.start()
1521
start_time = time.perf_counter()
1622

17-
result = func(*args, **kwargs)
23+
result = self.func(*args, **kwargs)
1824

1925
end_time = time.perf_counter()
2026
current, peak = tracemalloc.get_traced_memory()
2127
tracemalloc.stop()
2228

23-
wrapper.counter += 1
24-
wrapper.total_time += (end_time - start_time)
25-
wrapper.total_mem += peak
29+
self.counter += 1
30+
self.total_time += (end_time - start_time)
31+
self.total_mem += peak
2632

2733
return result
28-
29-
wrapper.counter = 0
30-
wrapper.total_time = 0.0
31-
wrapper.total_mem = 0
32-
33-
return wrapper

0 commit comments

Comments
 (0)