Skip to content

Commit f46ae7b

Browse files
authored
Merge pull request #686 from Yummy0945/patch-5
Create decorators_yagmur_tokdemir.py
2 parents 650b984 + 660e64f commit f46ae7b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import tracemalloc,time
2+
3+
4+
def performance(f):
5+
"""
6+
This function is a decorator used to measure the performance of a function.
7+
8+
Attributes:
9+
counter (int): The number of times the decorated function has been called.
10+
total_mem (int): The total peak memory usage across all calls to the decorated function.
11+
total_time (float): The total time taken by all calls to the decorated function in seconds.
12+
13+
Args:
14+
f (function): The function to be decorated.
15+
16+
Returns:
17+
function: The wrapper function that performs the tracking and calls the original function.
18+
19+
"""
20+
21+
setattr(performance, 'counter', 0)
22+
setattr(performance, 'total_mem', 0)
23+
setattr(performance, 'total_time', 0)
24+
25+
def wrapper(*args, **kwargs):
26+
tracemalloc.start()
27+
start_time = time.time()
28+
result = f(*args, **kwargs)
29+
end_time = time.time()
30+
current, peak = tracemalloc.get_traced_memory()
31+
tracemalloc.stop()
32+
setattr(performance, 'counter', getattr(performance, 'counter') + 1)
33+
setattr(performance, 'total_mem', getattr(performance, 'total_mem') + peak)
34+
setattr(performance, 'total_time', getattr(performance, 'total_time') + end_time - start_time)
35+
36+
return result
37+
38+
return wrapper

0 commit comments

Comments
 (0)