Skip to content

Commit e5b5a29

Browse files
Create decorators_fevzi_bagriacik.py
1 parent 71f5b39 commit e5b5a29

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import time
2+
import tracemalloc
3+
from functools import wraps
4+
5+
6+
def performance(func):
7+
"""
8+
A decorator which measures the performance of functions and
9+
also saves some statistics.
10+
"""
11+
12+
@wraps(func)
13+
def wrapper(*args, **kwargs):
14+
tracemalloc.start()
15+
start_time = time.perf_counter()
16+
17+
result = func(*args, **kwargs)
18+
19+
end_time = time.perf_counter()
20+
current, peak = tracemalloc.get_traced_memory()
21+
tracemalloc.stop()
22+
23+
wrapper.counter += 1
24+
wrapper.total_time += (end_time - start_time)
25+
wrapper.total_mem += peak
26+
27+
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)