Stopwatch(start=False, verbose=False, label=None, logger=None, logger_level=None)¶
Provides a set of methods and properties that you can use to accurately measure elapsed time.
- start (bool, optional) – if set to True, immediately starts measuring the time
start()). By default set to False.
- verbose (bool, optional) – if set to True, logs the elapsed time when the
stop()method is called. By default set to False.
- label (str, optional) – Optional stopwatch label to be included in the log messages (only if verbose is True).
- logger (
logging.Logger, optional) – logger object for logging stopwatch messages if verbose is True. If set to None (the default), the logger is set to
- logger_level (int, optional) – logging level as defined in the build-in logging package (only if the logger object is set).
Simple time measurement:
sw = Stopwatch(start=True) # code to be measured sw.stop()
Getting the elapsed time:
print(sw.elapsed) # hh:mm:ss.ms print(sw.elapsed.human_str()) # human-readable time
Restarting the stopwatch instance:
Pausing and resuming the stopwatch:
sw.suspend() # code block not included in the measurement sw.resume()
Using a logger:
import logging logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(logging.FileHandler(filename='example.log')) sw = Stopwatch(verbose=True, label='Example', logger=logger) sw.start() # code to be measured sw.stop()
Documentation of the
The total elapsed time measured by the current instance.
The total elapsed time in fractions of a second measured by the current instance.
Stops time interval measurement and resets the
Stopwatchinstance. The time elapsed before reset is set to zero.
Stops time interval measurement, resets the
Stopwatchinstance, and starts measuring elapsed time. The time elapsed before restart is set to zero.
Starts measuring elapsed time for an interval.
The time at which the time measurement has been started.
Stops the time measurement. Returns the total elapsed time measured by the current instance.
The time at which the time measurement has been stopped.
- start (bool, optional) – if set to True, immediately starts measuring the time (by calling