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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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