Skip to main content

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

Project description

TimeTap

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.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.timeTap_log("session"):
    compute_sum()
    time.sleep(0.1)
    compute_sum()


# Or print directly
TimeTap.timeTap_print()

# Reset collected metrics
TimeTap.timeTap_reset()

with TimeTap.timeTap_log("session-2"):
    compute_sum()

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

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

TimeTap.timeTap_print()

Output:

-----------------------------------------------------------------------------------------------
Function                 Runs    Total(ms)   Median(ms)      Avg(ms)      Min(ms)      Max(ms)
-----------------------------------------------------------------------------------------------
session                    1        132.6        132.6        132.6        132.6        132.6
└─compute-sum              2         32.4         16.2         16.2         16.2         16.2

WARNING:root:Disabling TimeTap timing measurements globally.
-----------------------------------------------------------------------------------------------
Function                 Runs    Total(ms)   Median(ms)      Avg(ms)      Min(ms)      Max(ms)
-----------------------------------------------------------------------------------------------
session-2                  1        250.9        250.9        250.9        250.9        250.9
└─compute-sum              2         33.7         16.8         16.8         16.7         17.0

Nested & GPU timing

import time
import TimeTap

# Nested sections automatically appear as a hierarchy in the report
with TimeTap.timeTap_log("pipeline"):
    with TimeTap.timeTap_log("stage-1"):
        with TimeTap.timeTap_log("stage-2"):
            with TimeTap.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.timeTap_log("forward-pass", gpu=True):
        time.sleep(0.02)

TimeTap.timeTap_print()

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.6        270.6        270.6        270.6        270.6
├─stage-1                  1        250.4        250.4        250.4        250.4        250.4
├───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:

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

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

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

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

  • disable_timeTap() / enable_timeTap() Globally toggle collection (no-op when disabled).

Project metadata

[project]
name = "TimeTap"
version = "0.1.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-0.1.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-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timetap-0.1.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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 89bc0c061631c02d093e0eae78423f21c5c320175660cdbe541371ecfb2898fc
MD5 da423ffa57c2333b1eb2cac30b7f12ae
BLAKE2b-256 255774e849459819853576f5908475ab29d9e819aae644f4a8a09831656e424b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timetap-0.1.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-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e00315cf00a814032facabc44d6500eb3bec9cd1f449600cf376aef700db1343
MD5 1dea7fae51dc795dbbce857938cb8581
BLAKE2b-256 afa873a72a4d29fa2731061632494f6d710b7c2063304d7fba21035b24e30bc3

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