TL;DR

tprof is a new profiler for Python 3.12+ that measures execution time only for specified target functions, reducing overhead and speeding iterative optimization. It supplies a command-line tool and a Python API with a comparison mode that reports relative speed changes between function versions.

What happened

A new tool called tprof has been introduced to help developers focus profiling on just the functions they care about. Traditional profilers collect program-wide metrics and can slow execution; tprof instead registers callbacks only for named target functions and performs timing in C, keeping the rest of the program free of profiler overhead. It is designed for Python 3.12 and leverages the new sys.monitoring API to trigger events for specific function executions. tprof offers a CLI that accepts multiple -t targets and a comparison flag to show deltas between functions, plus a Python API that works as a context manager or decorator. The author demonstrated using tprof to compare a manual loop implementation with a sum-based version, showing substantial speedup for the sum-based function. The package is available on PyPI and the author credits Mark Shannon for contributing sys.monitoring to CPython.

Why it matters

  • Targets profiling to specific functions so developers get focused measurements without re-profiling the whole program.
  • Minimizes runtime impact by registering callbacks only for selected functions and doing timing in C.
  • Includes a comparison mode to quantify performance changes between function variants quickly.
  • Provides both a command-line tool and a lightweight Python API for flexible integration into workflow.

Key facts

  • tprof requires Python 3.12 or later.
  • It profiles only user-specified target functions rather than the entire program.
  • tprof uses sys.monitoring to register callbacks for selected function executions.
  • Timing operations are performed in C to reduce measurement overhead.
  • Tooling includes a CLI with -t target flags and a compare mode that adds a delta column to reports.
  • A Python API is available as a context manager / decorator named tprof().
  • Example usage showed an 'after' implementation running roughly 60% faster than a 'before' implementation in the author’s benchmark.
  • The package is published on PyPI for installation.
  • The author acknowledged Mark Shannon for contributing sys.monitoring to CPython.

What to watch next

  • Adoption by the Python community and integration into developer workflows.
  • How tprof is used on larger, real-world projects to validate its low-overhead claims.
  • Support for Python versions earlier than 3.12 is not confirmed in the source.
  • Plans for further features or integration with other profiling and benchmarking tools are not confirmed in the source.

Quick glossary

  • Profiler: A tool that measures where a program spends time or resources to help identify performance bottlenecks.
  • Target function: A specific function selected by the developer for focused measurement during profiling.
  • sys.monitoring: A Python 3.12 API for registering callbacks when functions or lines of code execute, enabling targeted instrumentation.
  • Context manager: A Python construct (used with the with statement) that sets up and tears down a runtime context, often used for resource management or scoped behavior.
  • Decorator: A Python feature that wraps a function or method to modify or extend its behavior without changing its source code.

Reader FAQ

Which Python versions does tprof support?
tprof is built for Python 3.12 and later.

Does tprof add overhead to non-target code?
According to the author, tprof registers callbacks only for specified target functions via sys.monitoring, so it does not add overhead to the rest of the program.

Is there a command-line interface and a Python API?
Yes. tprof provides a CLI that accepts multiple targets and a Python API usable as a context manager or decorator.

Where can I install tprof?
The source states tprof is available on PyPI.

Is tprof included in CPython itself?
not confirmed in the source

Python: introducing tprof, a targeting profiler 2026-01-14 Profilers measure the performance of a whole program to identify where most of the time is spent. But once you’ve found a target…

Sources

Related posts

By

Leave a Reply

Your email address will not be published. Required fields are marked *