Skip to main content

Context manager for measuring execution times

Project description

This module provides a context manager for measuring execution times:

>>> from timethis import timethis
>>> with timethis('computing large sum'):
...     x = sum(range(10**7))
computing large sum: 0.203 seconds

Usage of timethis can be nested:

>>> with timethis('figuring things out'):
...     with timethis('computing large sum'):
...         x = sum(range(10**7))
...     with timethis('finding some primes'):
...         y = [2] + [i for i in range(3, 10**4) if all(i % j != 0 for j in range(2, i // 2 + 1))]
 computing large sum: 0.205 seconds
 finding some primes: 0.305 seconds
figuring things out: 0.510 seconds

It's easy to use (for example) logging instead of print:

>>> import logging
>>> with timethis('computing large sum', logging.warning):
...     x = sum(range(10**7))
WARNING:root:computing large sum: 0.218 seconds

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 timethis, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size timethis-0.1.1-py3-none-any.whl (2.2 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size timethis-0.1.1.tar.gz (1.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page