Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Week06/timer_murad_maharramli.py
Original file line number Diff line number Diff line change
@@ -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
Loading