-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_decorators.py
More file actions
35 lines (28 loc) · 973 Bytes
/
Copy pathcode_decorators.py
File metadata and controls
35 lines (28 loc) · 973 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
32
33
34
35
import time
from functools import wraps
def code_execution_time(func):
"""
Decorator to log the execution time of a function.
"""
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
# Calculate the length of the separator line
separator_length = 80
message_length = len(
f"* Execution time of {func.__name__}: {execution_time:.4f} seconds *"
)
padding_length = (separator_length - message_length) // 2
# Print the separator line and centered message
print("*" * separator_length)
print(
"*" * padding_length
+ f" Execution time of {func.__name__}: {execution_time:.4f} seconds "
+ "*" * padding_length
)
print("*" * separator_length)
return result
return wrapper