Skip to main content

Conveniently measures the time of loops, contexts and functions.

Project description

Horology

PyPI version tests codeql PythonVersion PythonVersion Downloads License: MIT

Conveniently measures the time of your loops, contexts and functions.

Installation

horology version compatible python
1.4 3.10-3.12
1.3 3.8-3.11
1.2 3.6-3.9
1.1 3.6-3.8

Horology can be installed with PIP. It has no dependencies.

pip install horology

Usage

The following 3 tools will let you measure practically any part of your Python code.

Timing an iterable (list, tuple, generator, etc)

Quick example

from horology import Timed

animals = ['cat', 'dog', 'crocodile']

for x in Timed(animals):
    feed(x)

Result:

iteration    1: 12.0 s
iteration    2: 8.00 s
iteration    3: 100 s

total 3 iterations in 120 s
min/median/max: 8.00/12.0/100 s
average (std): 40.0 (52.0) s

Customization

You can specify where (if at all) you want each iteration and summary to be printed, eg.:

for x in Timed(animals, unit='ms',
               iteration_print_fn=logger.debug,
               summary_print_fn=logger.info):
    feed(x)

Timing a function with a @timed decorator

Quick example

from horology import timed


@timed
def foo():
    ...

Result:

>>> foo()
foo: 7.12 ms

Customization

Chose time unit and name:

@timed(unit='s', name='Processing took ')
def bar():
    ...

Result:

>>> bar()
Processing took 0.185 s

Timing part of code with a Timing context

Quick example

Just wrap your code using a with statement

from horology import Timing

with Timing(name='Important calculations: '):
    ...

Result:

Important calculations: 12.4 s

Customization

You can suppress default printing and directly use measured time (also within context)

with Timing(print_fn=None) as t:
    ...

make_use_of(t.interval)

Time units

Time units are by default automatically adjusted, for example you will see foo: 7.12 ms rather than foo: 0.007 s. If you don't like it, you can override this by setting the unit argument with one of these names: ['ns', 'us', 'ms', 's', 'min', 'h', 'd'].

Contributions

Contributions are welcomed, see contribution guide.

Internals

Horology internally measures time with perf_counter which provides the highest available resolution, see docs.

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

horology-1.4.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

horology-1.4.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file horology-1.4.0.tar.gz.

File metadata

  • Download URL: horology-1.4.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Windows/10

File hashes

Hashes for horology-1.4.0.tar.gz
Algorithm Hash digest
SHA256 a3acc09bef46ec2139193de3af6c913eb17f58e7b090ccaa404cd53ca76ecc59
MD5 a59dea09b1e20b9e6f4e76fd5008511d
BLAKE2b-256 e1ab5d6d9b2dc96577926d47fb90ee0d887a74e1f4d93459647973eb5fb2f6d6

See more details on using hashes here.

File details

Details for the file horology-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: horology-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Windows/10

File hashes

Hashes for horology-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fbae96a30a9c04931fcb0242540018bb1af8b58b4335d3100b39775ee1d9c71
MD5 558af01a7598a18cac846d1537b9ba51
BLAKE2b-256 e3dc25dbc8bea5f0961456a83e5278c1025d350a11875df84d7e4f674b386f9c

See more details on using hashes here.

Supported by

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