Tprof,一个目标分析器
Python: Tprof, a Targeting Profiler

原始链接: https://adamj.eu/tech/2026/01/14/python-introducing-tprof/

## tprof:Python 的目标函数分析器 **tprof** 是一款新的 Python 3.12+ 分析器,旨在简化性能优化。与测量整个程序的传统分析器不同,tprof *仅* 关注指定的目标函数,从而显著降低开销并简化分析。 这使得开发者能够在修改代码*之前*和*之后*快速基准测试代码,以查看优化是否有效。tprof 提供命令行界面以生成快速报告,并提供“比较模式”以直接衡量函数之间的性能差异(例如,“之前”和“之后”版本),显示百分比提升/下降。 它还通过上下文管理器/装饰器提供 Python API,用于在特定代码块内进行分析。 tprof 利用 Python 新的 `sys.monitoring` API(在 3.12 中引入)和基于 C 的计时,以最大限度地减少性能影响,仅为被测函数添加开销。它在 PyPI 上可用,方便安装。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Python: Tprof,一个目标分析器 (adamj.eu) 9点 由 jonatron 3小时前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文
Can you hit your target?

Profilers measure the performance of a whole program to identify where most of the time is spent. But once you’ve found a target function, re-profiling the whole program to see if your changes helped can be slow and cumbersome. The profiler introduces overhead to execution and you have to pick out the stats for the one function you care about from the report. I have often gone through this loop while optimizing client or open source projects, such as when I optimized Django’s system checks framework (previous post).

The pain here inspired me to create tprof, a targeting profiler for Python 3.12+ that only measures the time spent in specified target functions. Use it to measure your program before and after an optimization to see if it made any difference, with a quick report on the command line.

For example, say you’ve realized that creating pathlib.Path objects is the bottleneck for your code. You could run tprof like so:

tprof in action measuring pathlib.Path performance.

Benchmark with comparison mode

Sometimes when optimizing code, you want to compare several functions, such as “before” and “after” versions of a function you’re optimizing. tprof supports this with its comparison mode, which adds a “delta” column to the report showing how much faster or slower each function is compared to a baseline.

For example, given this code:

def before():
    total = 0
    for i in range(100_000):
        total += i
    return total


def after():
    return sum(range(100_000))


for _ in range(100):
    before()
    after()

…you can run tprof like this to compare the two functions:

$ tprof -x -t before -t after -m example
🎯 tprof results:
 function         calls total  mean ± σ      min … max   delta
 example:before()   100 227ms   2ms ± 34μs   2ms … 2ms   -
 example:after()    100  86ms 856μs ± 15μs 835μs … 910μs -62.27%

The output shows that after() is about 60% faster than before(), in this case.

Python API

tprof also provides a Python API via a context manager / decorator, tprof(). Use it to profile functions within a specific block of code.

For example, to recreate the previous benchmarking example within a self-contained Python file:

from tprof import tprof


def before():
    total = 0
    for i in range(100_000):
        total += i
    return total


def after():
    return sum(range(100_000))


with tprof(before, after, compare=True):
    for _ in range(100):
        before()
        after()

…which produces output like:

$ python example.py
🎯 tprof results:
 function          calls total  mean ± σ      min … max delta
 __main__:before()   100 227ms   2ms ± 83μs   2ms … 3ms -
 __main__:after()    100  85ms 853μs ± 22μs 835μs … 1ms -62.35%

How it works

tprof uses Python’s sys.monitoring, a new API introduced in Python 3.12 for triggering events when functions or lines of code execute. sys.monitoring allows tprof to register callbacks for only specific target functions, meaning it adds no overhead to the rest of the program. Timing is done in C to further reduce overhead.

Thanks to Mark Shannon for contributing sys.monitoring to CPython! This is the second time I’ve used it—the first time was for tracking down an unexpected mutation (see previous post).

Fin

If tprof sounds useful to you, please give it a try and let me know what you think! Install tprof from PyPI with your favourite package manager.

May you hit your Q1 targets,

—Adam


😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸


One summary email a week, no spam, I pinky promise.

Related posts:

Tags:

© 2026 All rights reserved. Code samples are public domain unless otherwise noted.

联系我们 contact @ memedata.com