about-time
Easily measure timing and throughput of code blocks, with beautiful human friendly representations. It offers highly readable output for durations and counts, supporting various units from nanoseconds to days, and is particularly useful for profiling iterable operations like generators. The current version is 4.2.2, with active development indicated by frequent releases.
Warnings
- breaking Version 4.2.2 dropped support for Python 3.7. Ensure your environment is Python 3.8 or newer for compatibility.
- breaking Version 4.0.0 introduced significant changes to the API and output, including new global features, new objects for each operation, and simpler human-friendly representations. Code written for pre-4.0 versions may require adaptation.
- gotcha Prior to version 4.2.2, the `precision` parameter for output formatting was not truly optional, potentially causing unexpected behavior. This was fixed in 4.2.2.
Install
-
pip install about-time
Imports
- about_time
from about_time import about_time
Quickstart
import time
from about_time import about_time
# Example 1: Basic timing with a context manager
with about_time('Doing some work'):
time.sleep(0.123)
_ = [x * x for x in range(1000000)]
# Example 2: Timing an iterable (e.g., a generator)
def my_generator(n):
for i in range(n):
time.sleep(0.001)
yield i
with about_time('Processing generator items') as t:
total_sum = sum(my_generator(10))
print(f'Total sum: {total_sum}')
# Accessing detailed results after the block
print(f"Duration: {t.duration_human}")
print(f"Throughput: {t.throughput_human}")