timer.py

Simple utilities to measure elapsed time.

class ptlflow.utils.timer.Timer(name: str, indent_level: int = 0)[source]

Utility to count the total elapsed time.

Every time toc() is called, the elapsed time since the last tic() is added to the total time. Call reset() to zero the total time.

Examples

>>> t1 = Timer('parent_op', 0)
>>> t1.tic()
>>> ...
>>> t2 = Timer('inner_op', 1)
>>> t2.tic()
>>> ...
>>> t2.toc()
>>> t1.toc()
>>> print(t1)
parent_op: 2000.0 (2000.0) ms
>>> print(t2)
  inner_op: 1000.0 (1000.0) ms
Attributes:
namestr

A string name that will be printed to identify this timer.

indent_levelint, default 0

The level of indentation when printing this timer. Useful for creating better prints. E.g., inner parts may have a larger indentation.

__init__(name: str, indent_level: int = 0) None[source]

Initialize the Timer.

Parameters:
namestr

A string name that will be printed to identify this timer.

indent_levelint, optional

The level of indentation when printing this timer. Useful for creating better prints. E.g., inner parts may have a larger indentation.

mean() float[source]

Return the average time (total time divided by the number of tocs).

Returns:
float

The average time in milliseconds.

reset() None[source]

Zero the total time counter.

tic() None[source]

Start to count the elapsed time.

toc() None[source]

Count the elapsed time since the last tic() and add it to the total time.

total() float[source]

Return the total time since the last reset().

Returns:
float

The total time in milliseconds.

class ptlflow.utils.timer.TimerManager(log_id: str = 'timer', log_path: str = 'timer_log.txt')[source]

Utility to handle multiple timers.

Timers can be accessed using a dict-like call (see Usage below). The timers can be either printed to the default output or to a log file.

See also

Timer

The timers that are managed.

Examples

>>> tm = TimerManager()
>>> tm['op1'].tic()
>>> ...
>>> tm['op1'].toc()
>>> # You may pass a tuple (str, int) as a key, which will be interpreted
>>> # as the (name, indent_level) for the timer (see Timer above):
>>> tm[('op2', 1)].tic()
>>> ...
>>> tm['op2'].toc()
>>> print(tm)  # Prints all timers to default output
op1: 2000.0 (2000.0) ms
  op2: 1000.0 (1000.0) ms
>>> tm.write_to_log('Some header message (optional)')  # Write timers to a log file
Attributes:
timersdict[Union[str, tuple[str, int]], Timer]

The timers to be managed. The dict key can be either a single string representing the name of the timer, or a tuple (name, indentation_level)

log_idstr, default ‘timer’

A string representing the name of this manager.

log_pathstr, default ‘timer_log.txt’

Path to where the log file will be saved (if log is used).

loggerlogging.Logger

A hander for the logger.

__init__(log_id: str = 'timer', log_path: str = 'timer_log.txt') None[source]

Initialize the TimerManager.

Parameters:
log_idstr, default ‘timer’

A string representing the name of this manager.

log_pathstr, default ‘timer_log.txt’

Path to where the log file will be saved (if log is used).

clear() None[source]

Remove all timers.

reset() None[source]

Restart the total time counter of all timers.

write_to_log(header: str = '') None[source]

Write the timers to the log file.

Parameters:
headerstr

An optional string to be added to the top of the log file.