A timer context manager measuring the clock wall time of the code block it contains.
Likely abandoned. No release or commit for years (last release over 10 years ago).
pip install contexttimerA timer as a context manager
contexttimer provides you with a couple of utilities to quickly measure the execution time of a code block or a function.
Timer as a context manager
contexttimer.Timer is a context manager measuring the execution time of the code block it contains.
The elapsed time is accessible through the elapsed property.
with Timer() as t: ... # some code here print t.elapsed
a value in seconds
The contexttimer.Timer context manager
contexttimer.Timer is a context manager <http://docs.python.org/reference/datamodel.html#context-managers>_ with 2 parameters and a public property:
default_timer: a platform specific timer function (time.timefor Unix platforms andtime.clockfor Windows platforms). You can instanciate aTimerobject with your own timer, by passing it to the constructor.factor: a multiplying factor applied to theelapsedproperty. For example, a factor or 1000 will lead toelapsedbeing expressed in milliseconds instead of seconds. Default value of 1.elapsed: (read only property) the wall clock timing of the execution of the code block, in seconds. By default, expressed in seconds.
Example """""""
from contexttimer import Timer with Timer(factor=1000) as t: ... for i in xrange(10000000): ... pass ... print(t.elapsed) 73.6618041992 # in miliseconds
Note that elapsed is calculated on demand, so it is possible to time sub-parts of your code block:
with Timer(factor=1000) as t: ... # do some things ... print t.elapsed ... # do other tings ... print t.elapsed ... 10.122 # in ms 20.567
The contexttimer.timer function decorator
You can use the @timer function decorator to measure the time execution of an entire function.
When the function returns its value, its execution time will be printed to the stdout (default), or to the argument logger.
Examples """"""""