Skip to main content

A small library for high level measurement of code execution time

Project description

klokke

klokke is a small library for high level measurement of code execution time. klokke keeps track of running timers, and nests them according to execution order. This allows you to keep track of which sub-sections of code contributes the most to your overall time spent. In addition, a decorator @timed is provided for timing function execution.

Examples

Timing a snippet of code:

>>> from time import sleep
>>> from klokke import Timer
>>> with Timer("Something expensive") as timer:
...     sleep(5)
... 
>>> print(timer)
Something expensive: 5.004069805145264 seconds

Nesting timers:

>>> from time import sleep
>>> from klokke import Timer
>>> with Timer("outer") as outer:
...     with Timer("inner") as inner:
...         sleep(1)
...     sleep(2)
... 
>>> print(outer)
outer: 3.0084269046783447 seconds
Of which:
  inner: 1.0031757354736328 seconds

Using a decorator:

>>> from time import sleep
>>> from klokke import Timer, timed
>>> @timed
... def foo():
...     sleep(2)
...     return 5
... 
>>> with Timer("bar") as t:
...     x = foo() + foo()
... 
>>> print(t)
bar: 4.010395050048828 seconds
Of which:
  __main__.foo: 4.010124683380127 seconds

Setting up timers to automatically print on completion:

>>> from time import sleep
>>> from klokke import Timer
>>> with Timer("Something expensive", autoprint=print) as timer:
...     sleep(5)
... 
Something expensive: 5.000231981277466 seconds

All keyword arguments to Timer can also be passed through the @timed decorator:

>>> from time import sleep
>>> from klokke import timed
>>> import logging
>>> logger = logging.getLogger("foo")
>>> @timed(name="foo_log", autoprint=logger.warning)
... def foo():
...     sleep(1)
... 
>>> foo()
foo_log: 1.0042669773101807 seconds

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

klokke-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

klokke-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file klokke-0.1.0.tar.gz.

File metadata

  • Download URL: klokke-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.8 Darwin/22.1.0

File hashes

Hashes for klokke-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9266881bb1572395066cfc6224d1534be95e796ebd491d02dba50308dd842ab8
MD5 969d8daeac80fbc2193db8a5815496a7
BLAKE2b-256 ddfea24a02777ae27bb8766083380bf04577c949dcddfffe476e8b81f6f2cebd

See more details on using hashes here.

File details

Details for the file klokke-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: klokke-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.8 Darwin/22.1.0

File hashes

Hashes for klokke-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 879dd7bb3eabfb7604d45542139b6801feabc3e12c4632675a75e0fa06bcc3ed
MD5 161bd94343abc24b5a9cdecaf6b11b2e
BLAKE2b-256 1e942c1a516c50d0ce70c16fa920f1a7c2d4ac79c89cb9e6532e341060460c02

See more details on using hashes here.

Supported by

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