Skip to content

Commit 48403ad

Browse files
committed
📝 Update performance measurements section
* Add tprof
1 parent 1eb625d commit 48403ad

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

docs/performance/index.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ Performance-Messungen
6868
Wenn ihr erst einmal mit eurem Code gearbeitet habt, kann es nützlich sein, die
6969
Effizienz genauer zu untersuchen. Hierfür kann :abbr:`z. B. (zum Beispiel)`
7070
`cProfile <https://docs.python.org/3.14/library/profile.html#module-cProfile>`_,
71-
:doc:`ipython-profiler` oder :doc:`scalene` genutzt werden.
71+
:doc:`ipython-profiler`, :doc:`scalene` oder :doc:`tprof` genutzt werden.
72+
Bisher führe ich meist die folgenden Schritte aus:
73+
74+
#. Ich profilierte das gesamte Programm mit `cProfile
75+
<https://docs.python.org/3.14/library/profile.html#module-cProfile>`__ oder
76+
`py-spy <https://github.com/benfred/py-spy>`_, um langsame Funktionen zu
77+
finden.
78+
#. Anschließend optimiertee ich eine langsame Funktion.
79+
#. Schließlich erstellte ich ein neues Profil und filterte das Ergebnis meiner
80+
optimierten Version heraus um die Ergebnisse vergleichen zu können.
81+
82+
:mod:`sys.monitoring` in Python 3.12 vereinfachte das Monitoring bestimmter
83+
Funktionen. :doc:`tprof` nutzt dies, sodass im letzten Schritt nur noch das
84+
Profil für die optimierte Funktion erstellt werden muss.
7285

7386
.. versionadded:: Python3.15
7487
Mit :pep:`799` wird ein spezielles Profiling-Modul zur Verfügung stehen, das
@@ -98,6 +111,7 @@ Effizienz genauer zu untersuchen. Hierfür kann :abbr:`z. B. (zum Beispiel)`
98111

99112
ipython-profiler.ipynb
100113
scalene.ipynb
114+
tprof
101115
tachyon
102116

103117
Suche nach bestehenden Implementierungen

docs/performance/tprof.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)