Skip to main content

A handy set of tools to measure your code execution time

Project description

timeter

PyPI release Python supported versions License

timeter is a simple package with a handy set of tools to measure your code execution time. Having a plain function, a decorator and a context manager, it becomes easier to "timeit" in specific scenarios.

import functools
import logging
import operator

import timeter

logging.basicConfig(level=logging.INFO,  # don't forget to setup logging!
                    format='[{levelname}] {message}',
                    style='{')


def mult(*numbers: float) -> float:
    return functools.reduce(operator.mul, numbers, 1)


@timeter.timeter_decorator(format_spec='Computed factorial of {args} for {time_elapsed} seconds')
def factorial(n: int) -> int:
    return int(mult(*(range(2, n + 1))))


def pow(n: float, power: int, /) -> float:  # warning: incredibly slow
    return mult(*([n] * power))


timeter.timeter(mult, (2, 3, 4),
                number=10)  # [INFO] Function "mult(2, 3, 4)" executed 10 times for 6.999999999646178e-06 seconds
factorial(6)  # [INFO] Computed factorial of (6,) for 2.300000000003688e-06 seconds

n, power = 2, 2 ** 18
with timeter.Timer('math time', verbose=False) as t:
    pow(n, power)

print(f'Started at {t.startup_time=}')  # Started at t.startup_time=0.0384101
print(f'Finished at {t.end_time=}')  # Finished at t.end_time=1.1512166
print(f'{t.time_elapsed} seconds to compute pow({n}, {power})')  # 1.1128065 seconds to compute pow(2, 262144)

Install

pip install timeter

Usage

timeter contains three pieces:

  • timeter function to measure execution time for a single function:
timeter.timeter(mult, (2, 3, 4),
                number=10)  # [INFO] Function "mult(2, 3, 4)" executed 10 times for 6.999999999646178e-06 
  • timeter_decorator to measure each function's call execution time:
@timeter.timeter_decorator(format_spec='Computed factorial of {args} for {time_elapsed} seconds')
def factorial(n: int) -> int: ...
  • Timer class supporting context manager:
with timeter.Timer('math time', verbose=False, logging_level='DEBUG') as t:
    pow(n, power)

By default, the result is being print out into sys.stdout. Hence, you need to set up some basic logging with logging module to see the results. However, you can set verbose argument to False (except in timeter_decorator) to manipulate the resulted data by yourself.

You can also change how results get printed by changing format_spec. See code documentation to see available format values!

P.S.

This library is pretty experimental and may have a questionable API. If you have any suggestions - please leave them in issues.

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

timeter-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

timeter-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file timeter-1.0.0.tar.gz.

File metadata

  • Download URL: timeter-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for timeter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0769b45e81273ae44d0ba0c8a458c715a5282f13a78a7b636a613a1f14581dd4
MD5 93026cc9767fc0ee92d69b21ef3167a2
BLAKE2b-256 714e8fb74d7c3e4aea1399b3a9aa3eaf4c0db339b1b2aa740d5b71cb87e154ef

See more details on using hashes here.

File details

Details for the file timeter-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: timeter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for timeter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6da31b86f6004fce516295b88cf5ec431243efa915c5631808aaa5bd0fd5d26
MD5 fcb5fbe3387b23ebc2ed1a14286e035b
BLAKE2b-256 789ac2f22bd819068bbac0aa14f584530c95504c3bf9c0ffcced97eb88ba271f

See more details on using hashes here.

Supported by

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