Timer

class typhon.utils.Timer(info=None, verbose=True)[source]

Provide a simple time profiling utility

Parameters:
  • verbose (bool) – Print measured duration after stopping the timer.

  • info (str) – Allows to add additional information to output. The given string is printed before the measured time. If None, default information is added depending on the use case.

Returns:

The duration between start and end time.

Return type:

datetime.timedelta

Examples

Timer in with statement:

>>> import time
>>> with Timer():
...     time.sleep(1)
elapsed time: 0:00:01.003186

Timer as object (allows to store datetime.timedelta):

>>> import time
>>> t = Timer().start()
>>> time.sleep(1)
>>> dt = t.stop()
elapsed time: 0:00:01.004756

As function decorator:

>>> @Timer()
... def own_function(s):
...     import time
...     time.sleep(s)
>>> own_function(1)
own_function: 0:00:01.004667

Use it in format strings:

>>> from typhon.utils import Timer
>>> timer = Timer().start()
>>> print(f"{timer} elapsed")
0:00:00.000111 hours elapsed
__init__(info=None, verbose=True)[source]

Create a timer object.

Methods

__init__([info, verbose])

Create a timer object.

start()

Start timer.

stop()

Stop timer and print info message

Attributes

elapsed

Get the elapsed time as timedelta object