Small tool to track time of Python code blocks.
Project description
about-time
Small tool to track time of Python code blocks.
What does it do?
There are several times where we need to instrument and log code execution, to see where complex pipelines are spending the most time in.
A simple start = time.time()
and end = time.time() - start
does not cut it when we want to track several lines at the same time, and/or whole blocks with line granularity.
This tool measures the execution time of a block of code, and can now even count iterations and the throughput of them, always with a beautiful "human" representation.
How to use it?
There's three modes of operation: context manager, callable handler and iterator metrics.
1. Use it like a context manager:
from about_time import about_time
with about_time() as t_whole:
with about_time() as t_1:
func_1()
with about_time() as t_2:
func_2('params')
Then, get the timings like this:
print(f'func_1 time: {t_1.duration_human}')
print(f'func_2 time: {t_2.duration_human}')
print(f'total time: {t_whole.duration_human}')
There's also the duration
property, which returns the actual float time in seconds.
secs = t_whole.duration
2. You can also use it like a callable handler:
t_1 = about_time(func_1)
t_2 = about_time(lambda: func_2('params'))
If you use the callable handler syntax, there will be a new field called result
to get the outcome of the function!
results = t_1.result, t_2.result
Or you can mix and match both:
with about_time() as t_whole:
t_1 = about_time(func_1)
t_2 = about_time(lambda: func_2('params'))
3. And you can count and measure throughput:
Wrap your iterable and iterate it! Since it have duration, it can calculate the throughput of the whole block. Specially useful in generators, which do not have length, but you can use with any iterables:
def callback(t_func):
logger.info('func: size=%d throughput=%s', t_func.count,
t_func.throughput_human)
items = filter(...)
for item in about_time(callback, items):
# use item any way you want.
pass
How do I install it?
Just do in python env:
$ pip install about-time
Some nice features
Humans are first class citizens :)
I've considered two key concepts in designing the human friendly functions: 3.44s
is more meaningful than 3.43584783784s
, and 14.12us
is much nicer than .0000141233333s
. So saying it another way, I round values to two decimal places at most, and finds the smaller unit to represent it, minimizing values smaller than 1
.
Note that it dynamically finds the best unit to represent the value, considering even the rounding been applied. So if a value is for example 0.999999
, it would end up like 1000.0ms
after rounded, but it is auto-upgraded to the next unit 1.0s
.
The duration_human
changes seamlessly from nanoseconds to hours. Values smaller than 60 seconds are rendered with two digits precision at most (zeros to the right of the decimal point are not shown), and starting from 1 minute, an "hours:minutes:seconds.M" milliseconds (with only one digit precision). Some examples directly from the unit tests:
duration (float seconds) | duration_human |
---|---|
.00000000185 | '1.85ns' |
.000000999996 | '1.0us' |
.00001 | '10.0us' |
.0000156 | '15.6us' |
.01 | '10.0ms' |
.0141233333333 | '14.12ms' |
.1099999 | '110.0ms' |
.1599999 | '160.0ms' |
.8015 | '801.5ms' |
3.434999 | '3.43s' |
59.999 | '0:01:00' |
68.5 | '0:01:08.5' |
125.825 | '0:02:05.8' |
4488.395 | '1:14:48.4' |
The throughput_human
has similar logic, and to human brain it is much trickier to figure out: If it took 1165263
seconds to handle 123
items, how fast it did? Even dividing to find out the time per item 9473
seconds don't mean much. Dividing by 3600
we get 2.63
hours per item, and the throughput is returned nicely as 0.38/h
. The tool has per-second, per-minute and per-hour. Some examples:
duration (float seconds) | number of elements | throughput_human |
---|---|---|
1. | 10 | '10.0/s' |
1. | 2500 | '2500.0/s' |
2. | 1 | '30.0/m' |
2. | 10 | '5.0/s' |
1.981981981981982 | 11 | '5.55/s' |
100. | 10 | '6.0/m' |
1600. | 3 | '6.75/h' |
.99 | 1 | '1.01/s' |
1165263. | 123 | '0.38/h' |
Accuracy
This tool supports all versions of python, but in pythons >= 3.3
, the code uses the new time.perf_counter
to gain from the higher resolution and smaller propagating of errors. In older versions, it uses time.time
.
License
This software is licensed under the MIT License. See the LICENSE file in the top distribution directory for the full license text.
Nice huh?
Thanks for your interest!
I wish you have fun using this tool! :)
Project details
Release history Release notifications | RSS feed
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 about-time-2.0.4.tar.gz
.
File metadata
- Download URL: about-time-2.0.4.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f667adfa350dff845606a0dad142e55ae267bbdad82f4b73cdc24157cef9408b |
|
MD5 | 1aef99f0b20ee2d8f4817fdf71eaba5c |
|
BLAKE2b-256 | 46b38fc2bb89f23cdeca9e8e3beab65e724cd4d120128c66c1605c0b6e3da2cf |
File details
Details for the file about_time-2.0.4-py2-none-any.whl
.
File metadata
- Download URL: about_time-2.0.4-py2-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f943e04f0cce774eeda0d77280fc346bd4a9a10d0a12d2ba16e4de02484f911a |
|
MD5 | 3a621eb315dfdf839a937e90f9d8b241 |
|
BLAKE2b-256 | a12d455910a45124d6797141648a2e0d37509f13f84fdb4025d56f649848df64 |