Skip to main content

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
>>> from time import sleep
>>> with TimedContext() as tc:
...     sleep(0.5)
>>> round(tc.elapsed, 1)
0.5

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)
>>>
>>>  # rounding is needed here because of the variability of the system clock timing
>>> round(cumul_timing.elapsed, 1) == 0.6
True
>>> [round(t, 1) for t in 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((round(t, 1) for t in 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.5), (0.5, 0.8), (0.5, 2)])
>>>
>>> [round(t, 1) for t in timings]
[0.1, 0.4, 1.0]
>>> args
[(0.2, 0.5, 0.1), (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]))
>>>
>>> [round(t, 1) for t in 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():
...     sleep(0.5)
Took 0.5 seconds
>>> with TimerAndFeedback("doing something...", "... finished doing that thing"):
...     sleep(0.5)
doing something...
... finished doing that thing
Took 0.5 seconds
>>> with TimerAndFeedback(verbose=False) as feedback:
...     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(t.elapsed, 1) == 0.6
>>> # but the point of this demo is to show that cumul now holds all the timings
>>> assert [round(x, 1) for x in cumul] == [0.0, 0.2, 0.4, 0.6]

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lag-0.0.4.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lag-0.0.4-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file lag-0.0.4.tar.gz.

File metadata

  • Download URL: lag-0.0.4.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lag-0.0.4.tar.gz
Algorithm Hash digest
SHA256 6853e14758bf1e45f79d6b4028d21ef10bb65ddb3fcf4c455511fe24f76dbc9b
MD5 ef77c57d968ec6655feaca9821e0078c
BLAKE2b-256 43c422029b5e738b07713f2672022fb8565fa80c0c47d82ef44926ba9ddce014

See more details on using hashes here.

File details

Details for the file lag-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: lag-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lag-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1e69540d022cb5f776d82fce2d8a1a788129eba6d58918faa2e6c22c13352440
MD5 93c0f41368ef060fe96e215c48459672
BLAKE2b-256 3598d2d0c4fde95e4878beb70c6d6e8870388df27a23019215230ded2e800342

See more details on using hashes here.

Supported by

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