Вход на сайт

Просмотр новости

Найдите то, что Вас интересует

Python: Tprof, a Targeting Profiler

Дата публикации: 17-01-2026 07:30:06



Основное содержимое страницы с новостью.

2026-01-14Can 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


Learn how to make your tests run quickly in my book Speed Up Your Django Tests.


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

Related posts:

Tags: python

Схожие новости

#Наименование новостиТональностьИнформативностьДата публикации
1profiling.sampling: Statistical profiler01003-01-2026
2Introducing profiling-explorer043.3312-04-2026
3tdb: A Python Debugger Based on Textual01001-06-2026
4pytrendy: Trend Detection in Time Series Data01020-06-2026
5PyPy: A new benchmark runner for PyPy07.3520-06-2026
6syrupy: The Sweeter pytest Snapshot Plugin01003-04-2026
7Pyre: New JIT Python interpreter written in Rust01006-04-2026
8django-crawl - An in-process site crawler using Django’s test client028.1828-07-2026
9flake8-lazy: Detect Lazy-Importable Modules in Python 3.15+052.8601-06-2026
10PyInfra: Infrastructure Deserves Real Code in Python, Not YAML Soup01007-02-2026

Классификация: . Схожих патентов: 0. Схожих новостей: 10. Тональность: 0. Информативность: 10. Источник: pythondigest.ru.