-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathtimer_berke_alpaslan.py
62 lines (51 loc) · 2.07 KB
/
timer_berke_alpaslan.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
import time
class Timer:
"""
A context manager that measures the elapsed time of a code block.
This class allows you to measure the time taken to execute
a block of code using the context management protocol.
It records the start time when entering the context and
the end time when exiting the context, allowing you to
calculate and print the elapsed time.
Attributes:
start_time (float): The time when the timer starts.
end_time (float): The time when the timer ends.
Example:
>>> with Timer() as timer:
... time.sleep(2)
...
Elapsed time: 2.0000 seconds
"""
def __init__(self):
"""
Initializes the Timer with start and end times set to None.
Attributes:
start_time (float): The time when the timer starts (initially None).
end_time (float): The time when the timer ends (initially None).
"""
self.start_time = None
self.end_time = None
def __enter__(self):
"""
Starts the timer by recording the current time.
Returns:
Timer: The Timer instance, allowing access to start and end times.
"""
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_value, traceback):
"""
Stops the timer and calculates the elapsed time.
This method is called when exiting the context. It calculates
the elapsed time and prints it.
Args:
exc_type: The exception type (if an exception occurred).
exc_value: The exception value (if an exception occurred).
traceback: The traceback object (if an exception occurred).
Returns:
bool: Returns False to propagate any exceptions that occurred.
"""
self.end_time = time.time()
elapsed_time = self.end_time - self.start_time
print(f"\n\nElapsed time: {elapsed_time:.4f} seconds")
return False