about-time

4.2.2 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

Demonstrates basic usage with a context manager and advanced usage for profiling iterables/generators, showing human-readable output and direct attribute access.

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}")

view raw JSON →