-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathdecorators_heval_sogut.py
More file actions
39 lines (29 loc) · 1.07 KB
/
decorators_heval_sogut.py
File metadata and controls
39 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time
import tracemalloc
def performance(func):
# This is the wrapper function.
def wrapper(*args, **kwargs):
"""Wrapper function that adds performance tracking."""
# --- "Before" logic ---
tracemalloc.start()
start_time = time.perf_counter()
# Execute the original function
result = func(*args, **kwargs)
# --- "After" logic ---
# Get performance metrics
end_time = time.perf_counter()
_current_mem, peak_mem = tracemalloc.get_traced_memory()
tracemalloc.stop()
elapsed_time = end_time - start_time
# Update the statistics
performance.counter += 1
performance.total_time += elapsed_time
performance.total_mem += peak_mem
return result
# The decorator returns the new wrapper function
return wrapper
# Initialize the state as attributes of the function object.
# This state will be shared across all uses of the @performance decorator.
performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0