Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/syberiak/timeter
A handy set of tools to measure your code execution time.
https://github.com/syberiak/timeter
Last synced: 2 months ago
JSON representation
A handy set of tools to measure your code execution time.
- Host: GitHub
- URL: https://github.com/syberiak/timeter
- Owner: SyberiaK
- License: mit
- Created: 2024-06-01T14:07:09.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-06-01T14:13:57.000Z (8 months ago)
- Last Synced: 2024-11-10T10:06:31.643Z (3 months ago)
- Language: Python
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# timeter
[![PyPI release]][pypi]
[![Python supported versions]][pypi]
[![License]](./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.```python
import functools
import logging
import operatorimport 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 secondsn, 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:
```python
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:
```python
@timeter.timeter_decorator(format_spec='Computed factorial of {args} for {time_elapsed} seconds')
def factorial(n: int) -> int: ...
```- `Timer` class supporting context manager:
```python
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.[pypi]: https://pypi.org/project/timeter/
[PyPI Release]: https://img.shields.io/pypi/v/timeter.svg?label=pypi&color=green
[Python supported versions]: https://img.shields.io/pypi/pyversions/timeter.svg?label=%20&logo=python&logoColor=white
[License]: https://img.shields.io/pypi/l/timeter.svg?style=flat&label=license