Conveniently measures the time of loops, contexts and functions.
Project description
Horology
Conveniently measures the time of your loops, contexts and functions.
Installation
Simply:
pip install horology
Works with python versions 3.6, 3.7, 3.8 and 3.9. Tested on Linux, Windows and MacOS.
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
More cool stuff:
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
More cool stuff:
Personalize 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
More cool stuff:
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
Built Distribution
File details
Details for the file horology-1.2.0.tar.gz
.
File metadata
- Download URL: horology-1.2.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.10 CPython/3.7.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b4d8cbfd8206bee6535b537ee08f21cff155559ce9047072135643389c04506 |
|
MD5 | 1b2ba3675b950a2a4bb7a4f0d571341c |
|
BLAKE2b-256 | 72a2613a244f4ca2dd00e651bda6d83eb2febc797e7b1948cc9de7447c24ce58 |
File details
Details for the file horology-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: horology-1.2.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.10 CPython/3.7.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d69882e0bdfb85182c5f1b4fee204558ee07deaf6a5c62b4e71fae310d141faf |
|
MD5 | c8785b3e12e853b84ff9b44409bb2866 |
|
BLAKE2b-256 | afb23c4eb09b8dfb505c1d328139a5e24ad470242896ea8abe09c265b5204f15 |