|
| 1 | +.. SPDX-FileCopyrightText: 2026 Veit Schiele |
| 2 | +.. |
| 3 | +.. SPDX-License-Identifier: BSD-3-Clause |
| 4 | +
|
| 5 | +``tprof`` |
| 6 | +========= |
| 7 | + |
| 8 | +`tprof <https://github.com/adamchainz/tprof>`_ misst die Zeit, die beim |
| 9 | +Ausführen eines Moduls in bestimmten Funktionen verbracht wird. Im Gegensatz zu |
| 10 | +anderen Profilern verfolgt es nur die angegebenen Funktionen mit |
| 11 | +:mod:`sys.monitoring`, wodurch man sich das Filtern sparen kann. |
| 12 | + |
| 13 | +``tprof`` unterstützt die Verwendung als Befehlszeilenprogramm und mit einer |
| 14 | +Python-Schnittstelle: |
| 15 | + |
| 16 | +:samp:`uv run tprof -t {MODUL}:{FUNKTION} (-m {MODUL} | {PATH/TO/SCRIPT})` |
| 17 | + Angenommen, ihr habt festgestellt, dass die Erstellung von |
| 18 | + :class:`pathlib.Path`-Objekten im :mod:`main`-Modul euren Code verlangsamt. |
| 19 | + So könnt ihr dies mit ``tprof`` messen: |
| 20 | + |
| 21 | + .. code-block:: console |
| 22 | +
|
| 23 | + $ uv run tprof -t pathlib:Path.open -m main |
| 24 | + 🎯 tprof results: |
| 25 | + function calls total mean ± σ min … max |
| 26 | + pathlib:Path.open() 1 93μs 93μs 93μs … 93μs |
| 27 | +
|
| 28 | + Mit der ``-x``-Option könnt ihr auch zwei Funktionen miteinander |
| 29 | + vergleichen: |
| 30 | + |
| 31 | + .. code-block:: console |
| 32 | +
|
| 33 | + $ uv run tprof -x -t old -m main -t new -m main |
| 34 | + 🎯 tprof results: |
| 35 | + function calls total mean ± σ min … max delta |
| 36 | + main:old() 1 41μs 41μs 41μs … 41μs - |
| 37 | + main:new() 1 20μs 20μs 20μs … 20μs -50.67% |
| 38 | +
|
| 39 | +``tprof(*targets, label: str | None = None, compare: bool = False)`` |
| 40 | + verwendet diesen Code als :doc:`Kontextmanager |
| 41 | + <python-basics:control-flow/with>` oder :doc:`Dekorator |
| 42 | + <python-basics:functions/decorators>` in eurem Code, um ein Profiling in |
| 43 | + einem bestimmten Block durchzuführen. Der Bericht wird jedes Mal ausgegeben, |
| 44 | + wenn der Block durchlaufen wurde. |
| 45 | + |
| 46 | + ``*targets`` |
| 47 | + sind aufrufbare Elemente zum Profiling oder Referenzen zu Elementen, die |
| 48 | + mit :func:`pkgutil.resolve_name` aufgelöst werden. |
| 49 | + ``label`` |
| 50 | + ist eine optionale Zeichenfolge, die als Kopfzeile dem Bericht |
| 51 | + hinzugefügt werden kann. |
| 52 | + ``compare`` |
| 53 | + auf ``True`` gesetzt, wird der Vergleichsmodus aktiviert. |
| 54 | + |
| 55 | + Beispiel: |
| 56 | + |
| 57 | + .. code-block:: Python |
| 58 | +
|
| 59 | + from pathlib import Path |
| 60 | +
|
| 61 | + from tprof import tprof |
| 62 | +
|
| 63 | + with tprof(Path.open): |
| 64 | + p = Path("docs", "save-data", "myfile.txt") |
| 65 | + f = p.open() |
| 66 | +
|
| 67 | + .. code-block:: console |
| 68 | +
|
| 69 | + $ uv run python main.py |
| 70 | + 🎯 tprof results: |
| 71 | + function calls total mean ± σ min … max |
| 72 | + pathlib:Path.open() 1 82μs 82μs 82μs … 82μs |
0 commit comments