Skip to content

bpo-18795: Add cumpercall and totalpercall keys to pstats sort_stats #31919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 38 additions & 31 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,37 +402,41 @@ Analysis of the profiler data is done using the :class:`~pstats.Stats` class.

The following are the valid string and SortKey:

+------------------+---------------------+----------------------+
| Valid String Arg | Valid enum Arg | Meaning |
+==================+=====================+======================+
| ``'calls'`` | SortKey.CALLS | call count |
+------------------+---------------------+----------------------+
| ``'cumulative'`` | SortKey.CUMULATIVE | cumulative time |
+------------------+---------------------+----------------------+
| ``'cumtime'`` | N/A | cumulative time |
+------------------+---------------------+----------------------+
| ``'file'`` | N/A | file name |
+------------------+---------------------+----------------------+
| ``'filename'`` | SortKey.FILENAME | file name |
+------------------+---------------------+----------------------+
| ``'module'`` | N/A | file name |
+------------------+---------------------+----------------------+
| ``'ncalls'`` | N/A | call count |
+------------------+---------------------+----------------------+
| ``'pcalls'`` | SortKey.PCALLS | primitive call count |
+------------------+---------------------+----------------------+
| ``'line'`` | SortKey.LINE | line number |
+------------------+---------------------+----------------------+
| ``'name'`` | SortKey.NAME | function name |
+------------------+---------------------+----------------------+
| ``'nfl'`` | SortKey.NFL | name/file/line |
+------------------+---------------------+----------------------+
| ``'stdname'`` | SortKey.STDNAME | standard name |
+------------------+---------------------+----------------------+
| ``'time'`` | SortKey.TIME | internal time |
+------------------+---------------------+----------------------+
| ``'tottime'`` | N/A | internal time |
+------------------+---------------------+----------------------+
+--------------------+---------------------+--------------------------+
| Valid String Arg | Valid enum Arg | Meaning |
+====================+=====================+==========================+
| ``'calls'`` | SortKey.CALLS | call count |
+--------------------+---------------------+--------------------------+
| ``'cumulative'`` | SortKey.CUMULATIVE | cumulative time |
+--------------------+---------------------+--------------------------+
| ``'cumtime'`` | N/A | cumulative time |
+--------------------+---------------------+--------------------------+
| ``'file'`` | N/A | file name |
+--------------------+---------------------+--------------------------+
| ``'filename'`` | SortKey.FILENAME | file name |
+--------------------+---------------------+--------------------------+
| ``'module'`` | N/A | file name |
+--------------------+---------------------+--------------------------+
| ``'ncalls'`` | N/A | call count |
+--------------------+---------------------+--------------------------+
| ``'pcalls'`` | SortKey.PCALLS | primitive call count |
+--------------------+---------------------+--------------------------+
| ``'line'`` | SortKey.LINE | line number |
+--------------------+---------------------+--------------------------+
| ``'name'`` | SortKey.NAME | function name |
+--------------------+---------------------+--------------------------+
| ``'nfl'`` | SortKey.NFL | name/file/line |
+--------------------+---------------------+--------------------------+
| ``'stdname'`` | SortKey.STDNAME | standard name |
+--------------------+---------------------+--------------------------+
| ``'time'`` | SortKey.TIME | internal time |
+--------------------+---------------------+--------------------------+
| ``'tottime'`` | N/A | internal time |
+--------------------+---------------------+--------------------------+
| ``'cumpercall'`` | N/A | cumulative time per call |
+--------------------+---------------------+--------------------------+
| ``'totalpercall'`` | N/A | total time per call |
+--------------------+---------------------+--------------------------+

Note that all sorts on statistics are in descending order (placing most
time consuming items first), where as name, file, and line number searches
Expand All @@ -456,6 +460,9 @@ Analysis of the profiler data is done using the :class:`~pstats.Stats` class.
.. versionadded:: 3.7
Added the SortKey enum.

.. versionadded:: 3.11
Added the ``cumpercall`` and ``totalpercall`` keys.

.. method:: reverse_order()

This method for the :class:`Stats` class reverses the ordering of the
Expand Down
42 changes: 27 additions & 15 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,21 @@ def dump_stats(self, filename):
# list the tuple indices and directions for sorting,
# along with some printable description
sort_arg_dict_default = {
"calls" : (((1,-1), ), "call count"),
"ncalls" : (((1,-1), ), "call count"),
"cumtime" : (((3,-1), ), "cumulative time"),
"cumulative": (((3,-1), ), "cumulative time"),
"filename" : (((4, 1), ), "file name"),
"line" : (((5, 1), ), "line number"),
"module" : (((4, 1), ), "file name"),
"name" : (((6, 1), ), "function name"),
"nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
"pcalls" : (((0,-1), ), "primitive call count"),
"stdname" : (((7, 1), ), "standard name"),
"time" : (((2,-1), ), "internal time"),
"tottime" : (((2,-1), ), "internal time"),
"calls" : (((1,-1), ), "call count"),
"ncalls" : (((1,-1), ), "call count"),
"cumtime" : (((4,-1), ), "cumulative time"),
"cumulative" : (((4,-1), ), "cumulative time"),
"filename" : (((6, 1), ), "file name"),
"line" : (((7, 1), ), "line number"),
"module" : (((6, 1), ), "file name"),
"name" : (((8, 1), ), "function name"),
"nfl" : (((8, 1),(6, 1),(7, 1),), "name/file/line"),
"pcalls" : (((0,-1), ), "primitive call count"),
"stdname" : (((9, 1), ), "standard name"),
"time" : (((2,-1), ), "internal time"),
"tottime" : (((2,-1), ), "internal time"),
"cumpercall" : (((5,-1), ), "cumulative time per call"),
"totalpercall": (((3,-1), ), "total time per call"),
}

def get_sort_arg_defs(self):
Expand Down Expand Up @@ -263,8 +265,18 @@ def sort_stats(self, *field):

stats_list = []
for func, (cc, nc, tt, ct, callers) in self.stats.items():
stats_list.append((cc, nc, tt, ct) + func +
(func_std_string(func), func))
if nc == 0:
npc = 0
else:
npc = float(tt)/nc

if cc == 0:
cpc = 0
else:
cpc = float(ct)/cc

stats_list.append((cc, nc, tt, npc, ct, cpc) + func +
(func_std_string(func), func))

stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare))

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_sort_stats_int(self):
def test_sort_stats_string(self):
for sort_name in ['calls', 'ncalls', 'cumtime', 'cumulative',
'filename', 'line', 'module', 'name', 'nfl', 'pcalls',
'stdname', 'time', 'tottime']:
'stdname', 'time', 'tottime', 'cumpercall', 'totalpercall']:
self.stats.sort_stats(sort_name)
self.assertEqual(self.stats.sort_type,
self.stats.sort_arg_dict_default[sort_name][-1])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``cumpercall`` and ``totalpercall`` keys to pstats sort_stats