diff --git a/Week06/timer_murad_maharramli.py b/Week06/timer_murad_maharramli.py new file mode 100644 index 00000000..bc40b01f --- /dev/null +++ b/Week06/timer_murad_maharramli.py @@ -0,0 +1,24 @@ +import time + +class Timer: + """ + A context manager class that measures the execution time of a code block. + """ + def __init__(self): + # Initialize the required public attributes + self.start_time = None + self.end_time = None + + def __enter__(self): + # time.perf_counter() is used instead of time.time() for the highest + # available resolution when measuring short durations. + self.start_time = time.perf_counter() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + # Record the end time the moment the code block finishes + self.end_time = time.perf_counter() + + # Returning False ensures that if an exception occurs inside the 'with' block, + # it is not suppressed and raises normally. + return False