-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
31 lines (25 loc) · 971 Bytes
/
Copy pathhandler.py
File metadata and controls
31 lines (25 loc) · 971 Bytes
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
import time
from functools import wraps
class PerformanceLogger:
def __init__(self):
self.execution_times = []
def log_time(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
self.execution_times.append(end_time - start_time)
print(f'{func.__name__} executed in {end_time - start_time:.4f} seconds')
return result
return wrapper
def get_average_time(self):
return sum(self.execution_times) / len(self.execution_times) if self.execution_times else 0
performance_logger = PerformanceLogger()
@performance_logger.log_time
def compute_heavy_task(a, b):
time.sleep(2) # Simulating a heavy computation
return a + b
if __name__ == '__main__':
print(compute_heavy_task(5, 10))
print('Average execution time:', performance_logger.get_average_time())