-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathtimer_ege_enc.py
85 lines (69 loc) · 2.31 KB
/
timer_ege_enc.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import time
class Timer:
"""
This class calculates the time taken by a piece of code.
Attributes
----------
start_time : float
The start time of the block of code.
end_time : float
The end time of the block of code.
elapsed_time : float
The total time taken by the code block (in seconds).
Methods
-------
__enter__():
Records the start time and enters the context.
__exit__(exc_type, exc_val, exc_tb):
Records the end time, calculates the execution time, and exits the context.
__call__(task: str):
Prints the name of the task being executed.
"""
def __init__(self) -> None:
"""
Initializes an instance of Timer.
Returns:
None
"""
print(f"Initializing {self.__class__.__name__}")
self.start_time = None
self.end_time = None
self.elapsed_time = None # Initialize elapsed_time attribute
def __enter__(self) -> "Timer":
"""
Starts the timer by recording the start time.
Returns:
Timer: Returns itself
"""
print(f"Entering {self.__class__.__name__}")
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
"""
Stops the timer, calculates the execution time, and prints it.
Args:
exc_type: The type of exception raised, if any.
exc_val: The value of the exception, if any.
exc_tb: The traceback of the exception, if any.
Returns:
bool: True, to suppress any exception that occurs within the context.
"""
print(f"Exiting {self.__class__.__name__}")
self.end_time = time.time()
self.elapsed_time = self.end_time - self.start_time # Calculate and store elapsed time
print(f"Execution time: {self.elapsed_time:.4f} seconds")
return True
def __call__(self, task: str) -> None:
"""
Prints the name of the task being executed.
Args:
task (str): A string representing the task name.
"""
print(f"Calling {task}")
return None
def main() -> None:
with Timer() as calc:
for i in range(10 * 1000):
i ** 0.254444
if __name__ == "__main__":
main()