Skip to content

Commit d884fc5

Browse files
committed
Create decorators_ikram_celal_keskin.py
passed test on my computer but i need to try on github for is it true or not
1 parent 245311e commit d884fc5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import time
2+
import sys
3+
4+
5+
def performance(func):
6+
"""
7+
This function is a decorator that measures the performance of a function.
8+
This decorator function has a nested function called wrapper.
9+
It measures the time ,memory usage, and how many called of the this function.
10+
11+
:param func: function of the decorator
12+
:type func: function
13+
:return: wrapper of the decorator
14+
:rtype: function
15+
"""
16+
if not hasattr(performance, 'counter'):
17+
setattr(performance, 'counter', 0)
18+
19+
if not hasattr(performance, 'total_time'):
20+
setattr(performance, 'total_time', 0.0)
21+
22+
if not hasattr(performance, 'total_mem'):
23+
setattr(performance, 'total_mem', 0)
24+
25+
def wrapper(*args, **kwargs):
26+
"""
27+
This function is a wrapper that measures the performance of the decorated function.
28+
It measures the execution time, memory usage, and the number of calls to the function.
29+
30+
:param *args: Positional arguments to pass to the decorated function.
31+
:param **kwargs: Keyword arguments to pass to the decorated function.
32+
:return: The result of the decorated function.
33+
"""
34+
start_time = time.time()
35+
36+
#source https://www.geeksforgeeks.org/find-out-how-much-memory-is-being-used-by-an-object-in-python/
37+
memory_usage=sys.getsizeof(func(*args, **kwargs))
38+
end_time = time.time()
39+
40+
41+
setattr(performance, 'counter', getattr(performance, 'counter') + 1)
42+
setattr(performance, 'total_time', getattr(performance, 'total_time') + (end_time - start_time))
43+
setattr(performance, 'total_mem', getattr(performance, 'total_mem') + memory_usage)
44+
45+
return func(*args, **kwargs)
46+
47+
return wrapper
48+
49+

0 commit comments

Comments
 (0)