TicTocTimer

(class from pyomo.common.timing)

class pyomo.common.timing.TicTocTimer(ostream=NOTSET, logger=None)[source]

Bases: object

A class to calculate and report elapsed time.

Examples

>>> from pyomo.common.timing import TicTocTimer
>>> timer = TicTocTimer()
>>> timer.tic('starting timer') # starts the elapsed time timer (from 0)
[    0.00] starting timer
>>> # ... do task 1
>>> dT = timer.toc('task 1')
[+   0.00] task 1
>>> print("elapsed time: %0.1f" % dT)
elapsed time: 0.0

If no ostream or logger is provided, then output is printed to sys.stdout

Parameters:
  • ostream (FILE) – an optional output stream to print the timing information

  • logger (Logger) – an optional output stream using the python logging package. Note: the timing logged using logger.info()

__init__(ostream=NOTSET, logger=None)[source]

Methods

__init__([ostream, logger])

start()

stop()

tic([msg, ostream, logger, level])

Reset the tic/toc delta timer.

toc([msg, delta, ostream, logger, level])

Print out the elapsed time.

Member Documentation

tic(msg=NOTSET, *args, ostream=NOTSET, logger=NOTSET, level=NOTSET)[source]

Reset the tic/toc delta timer.

This resets the reference time from which the next delta time is calculated to the current time.

Parameters:
  • msg (str) – The message to print out. If not specified, then prints out “Resetting the tic/toc delta timer”; if msg is None, then no message is printed.

  • *args (tuple) – optional positional arguments used for %-formatting the msg

  • ostream (FILE) – an optional output stream (overrides the ostream provided when the class was constructed).

  • logger (Logger) – an optional output stream using the python logging package (overrides the ostream provided when the class was constructed). Note: timing logged using logger.info

  • level (int) – an optional logging output level.

toc(msg=NOTSET, *args, delta=True, ostream=NOTSET, logger=NOTSET, level=NOTSET)[source]

Print out the elapsed time.

This resets the reference time from which the next delta time is calculated to the current time.

Parameters:
  • msg (str) – The message to print out. If not specified, then print out the file name, line number, and function that called this method; if msg is None, then no message is printed.

  • *args (tuple) – optional positional arguments used for %-formatting the msg

  • delta (bool) – print out the elapsed wall clock time since the last call to tic() (False) or since the most recent call to either tic() or toc() (True (default)).

  • ostream (FILE) – an optional output stream (overrides the ostream provided when the class was constructed).

  • logger (Logger) – an optional output stream using the python logging package (overrides the ostream provided when the class was constructed). Note: timing logged using level

  • level (int) – an optional logging output level.