Skip to main content

Simplify logging of timings of selected parts of an application.

package version from PyPI build status from GitHub test coverage from Codecov grade from Codacy license

How to use

Recommended initialization is as follows.

import timing

_TIME = timing.get_timing_group(__name__)  # type: timing.TimingGroup

This follows the conventions of logging module.

import logging

_LOG = logging.getLogger(__name__)

Any name can be used instead of __name__. However, if names of format module.sub.sub_sub are used, this will create a timing hierarchy where each timing data is stored in its proper location and can be queried easier.

The resulting _TIME object is used to create individual timers, and will handle storing results in cache, which later can be used to obtain timing statistics.

You can obtain the timer object directly via start(name) method. You’ll need to manually call stop() in this case.

timer = _TIME.start('spam')  # type: timing.Timing
spam()
more_spam()
timer.stop()

You can also obtain the timer object indirectly via measure(name) context manager. The context manager will take care of calling stop() at the end.

with _TIME.measure('ham') as timer:  # type: timing.Timing
    ham()
    more_ham()

And if you want to time many repetitions of the same action (e.g. for statistical significance) you can use measure_many(name[, samples][, threshold]) generator.

You can decide how many times you want to measure via samples parameter and how many seconds at most you want to spend on measurements via threshold parameter

for timer in _TIME.measure_many('eggs', samples=1000):  # type: timing.Timing
    eggs()
    more_eggs()

for timer in _TIME.measure_many('bacon', threshold=0.5):  # type: timing.Timing
    bacon()
    more_bacon()

for timer in _TIME.measure_many('tomatoes', samples=500, threshold=0.5):  # type: timing.Timing
    tomatoes()
    more_tomatoes()

Also, you can use measure and measure(name) as decorator. In this scenario you cannot access the timings directly, but the results will be stored in the timing group object, as well as in the global cache unless you configure the timing to not use the cache.

import timing

_TIME = timing.get_timing_group(__name__)

@_TIME.measure
def recipe():
    ham()
    eggs()
    bacon()

@_TIME.measure('the_best_recipe')
def bad_recipe():
    spam()
    spam()
    spam()

Then, after calling each function the results can be accessed through summary property.

recipe()
bad_recipe()
bad_recipe()

assert _TIME.summary['recipe']['samples'] == 1
assert _TIME.summary['the_best_recipe']['samples'] == 2

The summary property is dynamically computed on first access. Subsequent accesses will not recompute the values, so if you need to access the updated results, call the summarize() method.

recipe()
assert _TIME.summary['recipe']['samples'] == 1

bad_recipe()
bad_recipe()
assert _TIME.summary['the_best_recipe']['samples'] == 2  # will fail
_TIME.summarize()
assert _TIME.summary['the_best_recipe']['samples'] == 2  # ok

Further API and documentation are in development.

See these examples in action in examples.ipynb notebook.

Requirements

Python version 3.11 or later.

Python libraries as specified in requirements.txt.

Building and running tests additionally requires packages listed in requirements_test.txt.

Tested on Linux, macOS and Windows.

Metadata

Release files for timing 0.6.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 timing 0.6.0
File Size Uploaded
timing-0.6.0.tar.gz 16.3 kB Details

Built distribution (wheel)

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

Total release size: 30.3 kB

Release files / timing-0.6.0.tar.gz

Download URL timing-0.6.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
75f341a3bb7a893bb78988f51220a1a0ca4dfaaaf2b69bb95fe49fa664b6c542
BLAKE2b-256 checksum
How to use checksums
8aa5edcb1374255e73de8ca8b67cf3ebc1c8a3ab9bdcc348b4154fb15e87bb57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / timing-0.6.0-py3-none-any.whl

Download URL timing-0.6.0-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b72eb85274d573badaa13cca30137ea9059c9d6f877ea998db7796518eebf847
BLAKE2b-256 checksum
How to use checksums
f30e6da554a67029ef45abb32485c3ec0d88c69571a127ef835ff7c04ce9adb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

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