Performance gauging tools
Project description
lag
Performance gauging tools.
Light weight, pure-python and only builtins (no further dependencies than python itself).
To install: pip install lag
Examples
TimedContext
is the base context manager of other context manager timers
that add some functionality to it: CumulativeTimings, TimerAndFeedback, TimerAndCallback
.
TimedContext
Starts a counter on enter and stores the elapsed time on exit.
>>> from lag import TimedContext, round_up_to_two_digits
>>> from time import sleep
>>> with TimedContext() as tc:
... sleep(0.5)
>>> round_up_to_two_digits(tc.elapsed)
CumulativeTimings
Context manager that is meant to be used in a loop to time and accumulate both timings and relevant data.
It's a context manager, but also a list (which will contain the an accumulation of the timings the instance encountered).
>>> from lag import CumulativeTimings
>>> from time import sleep
>>>
>>> cumul_timing = CumulativeTimings()
>>>
>>> for i in range(4):
... with cumul_timing:
... sleep(i * 0.2)
>>>
>>> # round_up_to_two_digits it needed here because of the variability of the system clock timing
>>> round_up_to_two_digits(cumul_timing.elapsed) == 0.6
True
>>> list(map(round_up_to_two_digits, cumul_timing))
[0.0, 0.2, 0.4, 0.6]
You can also add some data to the accumulation by calling the instance of CumulativeTimings Note: Calling cumul_timing to tell it to store some data for a loop step does have an overhead, so
>>> from lag import CumulativeTimings
>>> from time import sleep
>>> cumul_timing = CumulativeTimings()
>>> for i in range(4):
... with cumul_timing:
... sleep(i * 0.2)
... cumul_timing.append_data(f"index: {i}")
>>>
>>> list(zip(map(round_up_to_two_digits, cumul_timing), cumul_timing.data_store))
[(0.0, 'index: 0'), (0.2, 'index: 1'), (0.4, 'index: 2'), (0.6, 'index: 3')]
time_multiple_calls and time_arg_combinations
These functions use CumulativeTimings
to time a function call repeatedly with different inputs.
time_multiple_calls
feeds collections of arguments to a function,
measures how much time it takes to run, and output the timings (and possible function inputs and outputs).
>>> from lag import time_multiple_calls
>>> from time import sleep
>>> def func(i, j):
... t = i * j
... sleep(t)
... return t
>>> timings, args = time_multiple_calls(func, [(0.2, 0.3), (0.5, 0.8), (0.5, 2)])
>>>
>>> list(map(round_up_to_two_digits, timings))
[0.06, 0.4, 1.0]
>>> args
[(0.2, 0.3, 0.06), (0.5, 0.8, 0.4), (0.5, 2, 1.0)]
`time_arg_combinations' uses the above to feed combinations of arguments to a function.
>>> from lag import time_arg_combinations
>>> from time import sleep
>>> def func(i, j):
... t = i * j
... sleep(t)
... return t
>>> timings, args = time_arg_combinations(func, args_base=([0.1, 0.2], [2, 5]))
>>>
>>> list(map(round_up_to_two_digits, timings))
[0.2, 0.5, 0.4, 1.0]
>>> args
[(0.1, 2, 0.2), (0.1, 5, 0.5), (0.2, 2, 0.4), (0.2, 5, 1.0)]
TimerAndFeedback
Context manager that will serve as a timer, with custom feedback prints (or logging, etc.)
>>> from lag import TimerAndFeedback
>>> from time import sleep
>>> with TimerAndFeedback():
... time.sleep(0.5)
Took 0.5 seconds
>>> with TimerAndFeedback("doing something...", "... finished doing that thing"):
... time.sleep(0.5)
doing something...
... finished doing that thing
Took 0.5 seconds
>>> with TimerAndFeedback(verbose=False) as feedback:
... time.sleep(1)
>>> # but you still have access to some stats through feedback object (like elapsed, started, etc.)
TimerAndCallback
Context manager that will serve as a timer, with a custom callback called on exit
The callback is usually meant to have some side effect like logging or storing information.
>>> # run some loop, accumulating timing
>>> from lag import TimerAndCallback
>>> from time import sleep
>>> cumul = list()
>>> for i in range(4):
... with TimerAndCallback(cumul.append) as t:
... sleep(i * 0.2)
>>> # since system timing is not precise, we'll need to round our numbers to assert them, so:
>>> # See that you can always see what the timing was in the elapsed attribute
>>> assert round_up_to_two_digits(t.elapsed) == 0.6
>>> # but the point of this demo is to show that cumul now holds all the timings
>>> assert list(map(round_up_to_two_digits, cumul)) == [0.0, 0.2, 0.4, 0.6]
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 lag-0.0.3.tar.gz
.
File metadata
- Download URL: lag-0.0.3.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 585ba435d767864ad317f611cb8b4ae974825bc9243c857a4a22da46eb86ff62 |
|
MD5 | bf9ee96d14ff7b1d17ea6beb61bdc51f |
|
BLAKE2b-256 | a3d0c5b424aa6d5df62bcb3ba6a0fed5674b85edd3f79574b2a7c365f58deb32 |
File details
Details for the file lag-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: lag-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e4f1f22ea0fb890c89ca05b927bdf7d326a4a63e6bf9eea0407d25a732af91b |
|
MD5 | eba5fbebe261cb89618a889ad9680948 |
|
BLAKE2b-256 | 990cae375d95b8caf484f2923959febdae143d4b994c8cb48daf54f9ab814c6b |