Skip to main content

The only timing library you'll ever need.

Project description

timing

timing is a Python timing library providing timer functionality.

This library is one of many providing timers, but aims to:

  • incorporate all patterns for timing execution times
  • of functions and code blocks in any situation
  • while being minimalistic in complexity but powerful nonetheless.

Some of the library's features are:

  • It is compatible with Python 3.8+,
  • fully typed, allowing your IDE to provide useful completions,
  • tested using pytest and
  • well documented by docstrings

Example program:

import time
from timing import Timer

timer = Timer(callback=lambda x: print(f"Took {x:.2f} seconds."))
with timer:
    print("Executing step 1...")
    time.sleep(1)
with timer:
    print("Executing step 2...")
    time.sleep(2)

Output:

Executing step 1...
Took 1.00 seconds.
Executing step 2...
Took 2.00 seconds.

Installation

timing can be installed using

$ python -m pip install timering

Note that on some systems, the Python 3 executable is called python3 instead.

Release Notes

Supported Patterns

Start and Stop

Create a timer, start and stop it:

>>> from timing import Timer
>>> timer = Timer()
>>> timer.start()
>>> timer.stop()
2.442877164

Note that timing.start() creates a new Timer, starts and returns it directly.

Context

Create a timer and measure the execution duration of a context:

>>> with timer:
...     time.sleep(1)
...
>>> timer.get()
1.001198509

Measure Functions

Create a timer and measure a given function:

>>> import time
>>> timer.measure(lambda: time.sleep(2))
>>> timer.get()
2.002377642

Note that timing.measure() creates a new Timer and measures the given function with it. To retrieve the result, pass a callback to timing.measure() or use timing.prevtimer.get() if you are certain it wasn't replaced since the start of your timing.measure() call.

Decorator Pattern / Wrapping Functions

Create a timer and wrap a function to measure its execution duration:

>>> timed_func = timer.wrap(lambda: time.sleep(3))
>>> timed_func()
>>> timer.get()
3.003535439
>>> # alternatively
>>> @timer.wrap
... def timed_func():
...     time.sleep(2)
...
>>> timed_func()
>>> timer.get()
2.002481228

Note that there is also a timing.wrap() method available.

Decorator Generator Pattern

To use Python's @decorator pattern with arguments, libraries often provide functionality for generating decorators:

>>> @timer.wrap()
... def timed_func():
...     time.sleep(1)
...
>>> timed_func()
>>> timer.get()
1.001287624

Note that there is also a timing.wrap() method available.

Configuration

The Timer constructor accepts a unit used for the returned results and a callback called with the result everytime the timer stops. Every method also accepts the applicable arguments to override them once.

Be careful when calling stop() with a unit when a callback is defined as the callback will be called with the result in the new unit and not the one given to the constructor.

Contributing / Feedback

I happily accept feedback and pull requests.

Some ideas:

  • Should I add support for registering multiple callbacks with different units?

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

timering-1.0.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

timering-1.0.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file timering-1.0.2.tar.gz.

File metadata

  • Download URL: timering-1.0.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for timering-1.0.2.tar.gz
Algorithm Hash digest
SHA256 78b0d0f243dcf3f82b1745d7ce5844011dc68ef275e60282e43490458ad4e730
MD5 11db484e2ac77f89b29f63f66da7079f
BLAKE2b-256 a203e37d515698ae7f3582c81dd1246f0cbb6f58c96bff5e340a6be0b3244190

See more details on using hashes here.

File details

Details for the file timering-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: timering-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for timering-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 12e34d1bd832b7b8a8d29e3a772161f630776c3b40d56c73052030993bd3b8dd
MD5 f832a8878b5c4e606d4ceeff2d429831
BLAKE2b-256 23621a61aaa95773173c817e6bf054c7532a28a3d0c791494509278bbfb80590

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