Skip to content

Commit a0c379c

Browse files
committed
Document Python profiling features in PStats
1 parent a659a23 commit a0c379c

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

optimization/performance-issues/python-calculation.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ not mean you need to abandon Python and rewrite all your code in C++ for that
2020
single bottleneck. Only that single function or class can be ported to C++ and
2121
called from your Python code.
2222

23+
To determine whether it is the Python code that is slowing down an application,
24+
you should use PStats. You can enable the ``pstats-python-profiler`` Config.prc
25+
variable to get a detailed view of how much time each Python module, class and
26+
function is taking up. See :ref:`pstats-python-profiler` for more information.
27+
2328
Before considering writing any C++ code, consider other options:
2429

2530
- See if you can optimize your code. The Python website has a

optimization/pstats/basic-profiling.rst

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,51 @@ The PStats server interface will remain open, giving you a chance to further
6565
analyze the data or save it to disk. You need to close the existing session and
6666
click "New Session" before you can reconnect a new client.
6767

68-
Task Profiling
69-
--------------
68+
.. only:: python
7069

71-
If you are developing Python code, you may be interested in reporting the
72-
relative time spent within each Python task (by subdividing the total time spent
73-
in Python, as reported under "Show Code"). To do this, add the following lines
74-
to your Config.prc file before you start ShowBase:
70+
.. _pstats-python-profiler:
7571

76-
.. code-block:: text
72+
Profiling Python Code
73+
---------------------
74+
75+
If you are developing Python code, you may be interested in reporting the
76+
relative time spent within each Python task (by subdividing the total time
77+
spent in Python code, as reported under "App:Tasks"). To do this, add the
78+
following line to your Config.prc file before you start ShowBase:
79+
80+
.. code-block:: text
81+
82+
pstats-tasks 1
83+
84+
However, it can be even more useful to enable the Python profiler feature,
85+
which collects detailed information about the individual function calls made
86+
by the Python interpreter. There is a performance cost to this feature, which
87+
is why it is not enabled by default. To enable it, use the following setting:
88+
89+
.. code-block:: text
90+
91+
pstats-python-profiler 1
92+
93+
To get a hierarchical breakdown of the time taken by a particular Python
94+
module, class or function, use the Strip Chart view and double-click the App,
95+
then the Python label on the left side. Then, you can drill down into the
96+
specific Python packages, modules and functions:
97+
98+
.. image:: strip-chart-python-time.png
99+
:width: 1005
100+
101+
The time-based strip charts just collect the total time spent executing a
102+
particular function, without any information about what other functions a
103+
particular function is calling or is called by. Use the Flame Graph view to
104+
see call graph information:
105+
106+
.. image:: flame-graph-python.png
107+
:width: 747
77108

78-
task-timer-verbose 1
79-
pstats-tasks 1
109+
You can hover the mouse over the individual bars to see the full name, exact
110+
time and call count of the given function within its parent scope.
111+
Double-clicking a bar will narrow down the view to only that function and any
112+
functions called by it, and double-clicking the white space will go back.
80113

81114
Remote Profiling
82115
----------------
52.4 KB
Loading
84.2 KB
Loading

0 commit comments

Comments
 (0)