diff --git a/Week04/decorators_murad_maharramli.py b/Week04/decorators_murad_maharramli.py new file mode 100644 index 00000000..1e68d2e5 --- /dev/null +++ b/Week04/decorators_murad_maharramli.py @@ -0,0 +1,32 @@ +import time +import tracemalloc +from functools import wraps + +def performance(func): + """ + A decorator that measures the performance of functions and saves statistics. + """ + @wraps(func) + def wrapper(*args, **kwargs): + performance.counter += 1 + + tracemalloc.start() + start_time = time.perf_counter() + + result = func(*args, **kwargs) + + end_time = time.perf_counter() + _, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.total_time += (end_time - start_time) + performance.total_mem += peak_mem + + return result + + return wrapper + +# Initialize the required tracking attributes on the decorator +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0