chrono — A context manager to time code sections

This module provides the Chrono context manager.

class valjean.chrono.Chrono[source]

Time some code and store the elapsed time.

This class is a simple context manager that measures the execution time of a code fragment. The elapsed time is stored as a float in self.elapsed and can be accessed as such.

Examples:

>>> with Chrono() as chrono:
...     print(42)
42
>>> print(f'printing 42 took {chrono} seconds')
printing 42 took ... seconds
__init__()[source]
__str__()[source]

Return the elapsed time, as a string.

__float__()[source]

Return the elapsed time, as a float.

__int__()[source]

Return the elapsed time, as an int.

__format__(format_spec)[source]

Return the elapsed time, formatted according to format_spec.

>>> chrono = Chrono()
>>> chrono.elapsed = 1e-6
>>> f'{chrono}'
'1e-06'
>>> f'{chrono:f}'
'0.000001'