Source code for pyforce.tools.timer

# Auxiliary Timer Class
# Latest Code Update: 05 March 2024
# Latest Doc  Update: 05 March 2024

import time

[docs] class TimerError(Exception): """A custom exception used to report errors in use of Timer class"""
[docs] class Timer: def __init__(self): self._start_time = None
[docs] def start(self): """Start a new timer""" if self._start_time is not None: raise TimerError(f"Timer is running. Use .stop() to stop it") self._start_time = time.process_time()
[docs] def stop(self): """Stop the timer, and report the elapsed time""" if self._start_time is None: raise TimerError(f"Timer is not running. Use .start() to start it") elapsed_time = time.process_time() - self._start_time self._start_time = None return elapsed_time