TimeRun - Python library for elapsed time measurement.
TimeRun is a simple, yet elegant elapsed time measurement library for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.
- Elapsed Time: Customized time delta which represents elapsed time in nanoseconds
- Stopwatch: An elapsed time measurer with the highest available resolution
- Timer: Convenient syntax to capture and save measured elapsed time results
Setup
Prerequisites
The only prerequisite to use TimeRun is running Python 3.9+.
Installation
Install TimeRun from Python Package Index:
pip install timerun
Install TimeRun from Source Code:
pip install git+https://github.com/HH-MWB/timerun.git
Quickstart
Measure Code Block
>>> import time
>>> from timerun import Timer
>>> with Timer() as timer:
... time.sleep(0.1)
>>> print(timer.duration)
0:00:00.100000000
Measure Function
>>> import time
>>> from timerun import Timer
>>> timer = Timer()
>>> @timer
... def func():
... time.sleep(0.1)
>>> func()
>>> print(timer.duration)
0:00:00.100000000
Measure Async Function
>>> import asyncio
>>> from timerun import Timer
>>> timer = Timer()
>>> @timer
... async def async_func():
... await asyncio.sleep(0.1)
>>> asyncio.run(async_func())
>>> print(timer.duration)
0:00:00.100000000
Measure Async Code Block
>>> import asyncio
>>> from timerun import Timer
>>> async def async_code():
... async with Timer() as timer:
... await asyncio.sleep(0.1)
... print(timer.duration)
>>> asyncio.run(async_code())
0:00:00.100000000
Multiple Measurements
>>> import time
>>> from timerun import Timer
>>> timer = Timer()
>>> with timer:
... time.sleep(0.1)
>>> with timer:
... time.sleep(0.1)
>>> print(timer.duration)
0:00:00.100000000
>>> print(timer.durations)
(ElapsedTime(nanoseconds=100000000), ElapsedTime(nanoseconds=100000000))
Advanced Options
>>> from timerun import Timer
>>>
>>> timer = Timer(count_sleep=False)
>>>
>>> timer = Timer(max_len=10)
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines on how to contribute to this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.