Skip to main content

A timer context manager measuring the clock wall time of the code block it contains.

Project description

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 with 2 parameters and a public property:

  • default_timer: a platform specific timer function (time.time for Unix platforms and time.clock for Windows platforms). You can instanciate a Timer object with your own timer, by passing it to the constructor.

  • factor: a multiplying factor applied to the elapsed property. For example, a factor or 1000 will lead to elapsed being 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() 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() 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 fuction. When the function returns its value, its execution time will be printed to the stdout (default), or to the argument logger.

Examples

>>> @timer
... def sleep_for_2s():
...     time.sleep(2)
>>> sleep_for_2s()
function sleep_for_2s execution time: 2.002
>>> logging.basicConfig()
>>> @timer(logger=logging.getLogger())
... def sleep_for_2s():
...     time.sleep(2)
>>> sleep_for_2s()
DEBUG:root:function blah execution time: 2.002

As it makes use of the Timer context manager inside, all arguments passed to the @timer decorator will be used a Timer init arguments.

Example:

>>> @timer(factor=1000)
... def sleepawhile(n):
...     time.sleep(n)
...
>>> sleepawhile(2)
function sleepawhile execution time: 2000.089

Thanks

Thanks to halloi, wolanko and Jon Blackburn for their helpful insights and contributions.

License

contexttimer is released under the GPLv3 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contexttimer-0.2.1.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file contexttimer-0.2.1.tar.gz.

File metadata

File hashes

Hashes for contexttimer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 47c05071f47e6c742da0b14c32828f840fd9dd412290b0afae1e0bbc211bcfac
MD5 6e92a5af748edcf04f7a3827bd7752d1
BLAKE2b-256 2490bcbe2fea81751df3742b90d3e93250529f7a3c934d10562b71110b0da5a5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page