API

Module et_stopwatch

A class for timing a piece of code.

Inspiration taken from Python Timer Functions: Three Ways to Monitor Your Code

class et_stopwatch.Stopwatch(message='Stopwatch', ndigits=6, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, stats=False)[source]

Class for timing code fragments.

Constructor parameters:

Parameters
  • message (str) – this text will appear when the Stopwatch object is printed

  • ndigits (int) – number of digits in returned or printed timings.

start(message=None)[source]

Start or restart this Stopwatch object.

Parameters

message (str) – modify the message used when the Stopwatch object is printed.

stop(stats=True)[source]

Stop the stopwatch.

Parameters

stats (bool) – if False no statistics are acummulated.

Returns

the number of seconds (float) since the most recent call to stop or start.

Note

Stop() calls start() immediately before returning. This is practical in an iteration, but as such includes the overhead of the iteration. Call start() explicitly to avoid this as in:

with Stopwatch(message='This took') as sw:
    for i in range(3):
        sw.start()               # restart the stopwatch
        sleep(1)                 # only this is timed
        print(i, sw.stop(), 's') # stop the stopwatch and returns second since start
property time

The number of seconds as measured in the most recent call to stop().