Skip to main content

A lightweight, thread-safe Python profiler for hierarchical code timing with GPU support

Project description

TimeTap - Tap Into Runtime

A lightweight, thread-safe Python profiler for hierarchical code timing — with optional GPU support.

  • Hierarchical timings: nest blocks, functions, and lines.
  • Two ways to use: decorator or with context manager.
  • Thread-safe: collect timings across threads.
  • Optional GPU support to measure PyTorch CUDA sections (install extra).
  • Easy reporting: print or get a formatted string; reset anytime.

Installation

# CPU-only
pip install timetap

# With GPU helpers (PyTorch >= 2.1)
pip install "timetap[torch]"

Quick start

import time
import timetap  # package exposes timing utilities


@timetap.log(name="compute-sum")
def compute_sum(n=1_000_000):
    total = 0
    for i in range(n):
        total += i
    return total


with timetap.log("session"):
    compute_sum()
    time.sleep(0.1)
    compute_sum()


# Or print directly
timetap.print_table()

# Reset collected metrics
timetap.reset()

with timetap.log("session-2"):
    compute_sum()

    # You can temporarily disable collection any time
    timetap.disable()
    time.sleep(0.1)
    compute_sum()

    # And re-enable all again
    timetap.enable()
    time.sleep(0.1)
    compute_sum()

timetap.print_table()

Output:

------------------------------------------------------------------------------------------
Function            Runs    Total(ms)   Median(ms)      Avg(ms)      Min(ms)      Max(ms)
------------------------------------------------------------------------------------------
session               1        134.7        134.7        134.7        134.7        134.7
└─compute-sum         2         34.5         17.3         17.3         17.0         17.6

WARNING:root:Disabling TimeTap timing measurements globally.
------------------------------------------------------------------------------------------
Function            Runs    Total(ms)   Median(ms)      Avg(ms)      Min(ms)      Max(ms)
------------------------------------------------------------------------------------------
session-2             1        252.0        252.0        252.0        252.0        252.0
└─compute-sum         2         34.2         17.1         17.1         16.8         17.4

Nested & GPU timing

import time
import timetap

# Nested sections automatically appear as a hierarchy in the report
with timetap.log("pipeline"):
    with timetap.log("stage-1"):
        with timetap.log("stage-2"):
            with timetap.log("stage-3"):
                time.sleep(0.1)
            time.sleep(0.1)
        time.sleep(0.05)

    # If installed with timetap[torch], set gpu=True to time CUDA work
    with timetap.log("forward-pass", gpu=True):
        time.sleep(0.02)

timetap.print_table()

Tip: When gpu=True and PyTorch is available, TimeTap accounts for CUDA work so your timings include GPU kernels.

Output:

------------------------------------------------------------------------------------------
Function            Runs    Total(ms)   Median(ms)      Avg(ms)      Min(ms)      Max(ms)
------------------------------------------------------------------------------------------
pipeline              1        270.5        270.5        270.5        270.5        270.5
├─stage-1             1        250.3        250.3        250.3        250.3        250.3
├───stage-2           1        200.2        200.2        200.2        200.2        200.2
├─────stage-3         1        100.1        100.1        100.1        100.1        100.1
└─forward-pass        1         20.1         20.1         20.1         20.1         20.1

API

All functions are available off the TimeTap package:

  • log(name: str, enable: bool = True, verbose: bool = False, gpu: bool = False)

    • Decorator or context manager for timing.
    • As a decorator: @TimeTap.log(name="...")
    • As a context manager: with TimeTap.log("..."): ...
  • get_table_str() -> str Get a formatted, hierarchical summary of collected timings.

  • print_table() -> None Print the current summary to stdout.

  • reset() -> None Clear all collected metrics.

  • disable() / enable() Globally toggle collection (no-op when disabled).

Project metadata

[project]
name = "timetap"
version = "1.0.0"
description = "A lightweight, thread-safe Python profiler for hierarchical code timing with GPU support"
readme = "README.md"
requires-python = ">=3.8"
dependencies = []

[project.optional-dependencies]
torch = ["torch>=2.1.0"]

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

timetap-1.0.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timetap-1.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file timetap-1.0.0.tar.gz.

File metadata

  • Download URL: timetap-1.0.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for timetap-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0c369bf5428dffdd24e33a24baeb7b936e0b35caabd8c4938f161556e51e8947
MD5 1de18e3fbedfe00ddc9bc5728309cb7f
BLAKE2b-256 b842379d758937fd0ad0616e8135e55645e3ac30a807ba98c76247fde7e1ebd8

See more details on using hashes here.

File details

Details for the file timetap-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: timetap-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for timetap-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3381dbc166b9a44133efc76baecef222eea59229e7b2503ef702b7283d0868c
MD5 5ee039c19d951289a584caf04119afea
BLAKE2b-256 2215a0d7753b93359dc75fbc1619cb1e585ed84dda0335820180b67dda0c73ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page