Skip to main content

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

Release files for klokke 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for klokke 0.1.0
File Size Uploaded
klokke-0.1.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for klokke 0.1.0
File Interpreter ABI Platform
klokke-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.1 kB

Release files / klokke-0.1.0.tar.gz

Download URL klokke-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9266881bb1572395066cfc6224d1534be95e796ebd491d02dba50308dd842ab8
BLAKE2b-256 checksum
How to use checksums
ddfea24a02777ae27bb8766083380bf04577c949dcddfffe476e8b81f6f2cebd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.8.8 Darwin/22.1.0

Release files / klokke-0.1.0-py3-none-any.whl

Download URL klokke-0.1.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
879dd7bb3eabfb7604d45542139b6801feabc3e12c4632675a75e0fa06bcc3ed
BLAKE2b-256 checksum
How to use checksums
1e942c1a516c50d0ce70c16fa920f1a7c2d4ac79c89cb9e6532e341060460c02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.8.8 Darwin/22.1.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page