Skip to main content

Time a block of code

Project description


Time a block of code.

version python version license build code style

Use as the context expression of a with statement:

>>> from harrison import Timer
>>> with Timer() as t:
>>>     ...
>>> print(t.elapsed_time_ms)

When a description string is passed on initialization, the elapsed time will be printed on completion, keyed by this description.

>>> with Timer('My expensive block of code'):
>>>     ...
My expensive block of code: 12345 ms

You can also start and stop a Timer explicitly:

timer = Timer()



You can also time each execution of a function using a decorator:

from harrison import profile

@profile('Describes the function')
def some_function():

# Without args, the function name (e.g. 'some_function') will be used
# as the description.
def another_function():

You can also use RegisteredTimer, which groups together a bunch of named timers, provides utilities for serializing their times, and an optional global timer registry.

Named after John Harrison, the English carpenter and clockmaker who invented the marine chronometer.

Similar libraries

This is similar to the library contexttimer, but that library is licensed under the GPLv3 which is more restrictive than two-clause BSD license used here.


./ init
./ test


Pull requests welcome!


If you are having issues, please let us know.


This project was packaged by Paul Melnikow while at Body Labs. Thanks to Body Labs for the repository transfer.


The project is licensed under the two-clause BSD license.

Project details

Download files

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

Files for harrison, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size harrison-2.0.0-py3-none-any.whl (8.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size harrison-2.0.0.tar.gz (6.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page