timeutils.stopwatch module

class timeutils.stopwatch.Stopwatch(start=False)[source]

Provides a set of methods and properties that you can use to accurately measure elapsed time.

Parameters:start (bool) – if set to True, immediately starts measuring the time (by calling start()). By default set to False.

Examples

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:

sw.restart()

Pausing and resuming the stopwatch:

sw.pause()
# code block not included in the measurement
sw.resume()

Note

Stopwatch methods are protected against inappropriate calls. It is an error to start or stop a Stopwatch object that is already in the desired state.

See also

Documentation of the TimeSpan class.

elapsed

Gets the total elapsed time measured by the current instance.

elapsed_seconds

Gets the total elapsed time in fractions of a second measured by the current instance.

is_paused

Indicates whether the Stopwatch instance is paused.

is_running

Indicates whether the Stopwatch instance is running.

pause()[source]

Pauses the time measurement until the Stopwatch instance is resumed or stopped. Returns the total elapsed time measured by the current instance.

Call resume() to resume measuring elapsed time.

reset()[source]

Stops time interval measurement and resets the Stopwatch instance. The time elapsed before reset is set to zero.

restart()[source]

Stops time interval measurement, resets the Stopwatch instance, and starts measuring elapsed time. The time elapsed before restart is set to zero.

resume()[source]

Resumes measuring elapsed time after calling pause().

start()[source]

Starts measuring elapsed time for an interval.

start_time

Returns the time at which the time measurement has been started.

stop()[source]

Stops the time measurement. Returns the total elapsed time measured by the current instance.

stop_time

Returns the time at which the time measurement has been stopped.