Skip to content

Commit b672f5c

Browse files
committed
bpo-18795: Add cumpercall and totalpercall keys to pstats sort_stats
Co-authored-by: Alexandre Dias
1 parent cfeede8 commit b672f5c

File tree

4 files changed

+67
-47
lines changed

4 files changed

+67
-47
lines changed

Doc/library/pstats.rst

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -186,37 +186,41 @@ The :class:`!Stats` class
186186

187187
Valid sort keys:
188188

189-
+------------------+------------------------+----------------------+
190-
| String | Enum | Meaning |
191-
+==================+========================+======================+
192-
| ``'calls'`` | ``SortKey.CALLS`` | call count |
193-
+------------------+------------------------+----------------------+
194-
| ``'cumulative'`` | ``SortKey.CUMULATIVE`` | cumulative time |
195-
+------------------+------------------------+----------------------+
196-
| ``'cumtime'`` | N/A | cumulative time |
197-
+------------------+------------------------+----------------------+
198-
| ``'file'`` | N/A | file name |
199-
+------------------+------------------------+----------------------+
200-
| ``'filename'`` | ``SortKey.FILENAME`` | file name |
201-
+------------------+------------------------+----------------------+
202-
| ``'module'`` | N/A | file name |
203-
+------------------+------------------------+----------------------+
204-
| ``'ncalls'`` | N/A | call count |
205-
+------------------+------------------------+----------------------+
206-
| ``'pcalls'`` | ``SortKey.PCALLS`` | primitive call count |
207-
+------------------+------------------------+----------------------+
208-
| ``'line'`` | ``SortKey.LINE`` | line number |
209-
+------------------+------------------------+----------------------+
210-
| ``'name'`` | ``SortKey.NAME`` | function name |
211-
+------------------+------------------------+----------------------+
212-
| ``'nfl'`` | ``SortKey.NFL`` | name/file/line |
213-
+------------------+------------------------+----------------------+
214-
| ``'stdname'`` | ``SortKey.STDNAME`` | standard name |
215-
+------------------+------------------------+----------------------+
216-
| ``'time'`` | ``SortKey.TIME`` | internal time |
217-
+------------------+------------------------+----------------------+
218-
| ``'tottime'`` | N/A | internal time |
219-
+------------------+------------------------+----------------------+
189+
+--------------------+------------------------+--------------------------+
190+
| String | Enum | Meaning |
191+
+====================+========================+==========================+
192+
| ``'calls'`` | ``SortKey.CALLS`` | call count |
193+
+--------------------+------------------------+--------------------------+
194+
| ``'cumulative'`` | ``SortKey.CUMULATIVE`` | cumulative time |
195+
+--------------------+------------------------+--------------------------+
196+
| ``'cumtime'`` | N/A | cumulative time |
197+
+--------------------+------------------------+--------------------------+
198+
| ``'file'`` | N/A | file name |
199+
+--------------------+------------------------+--------------------------+
200+
| ``'filename'`` | ``SortKey.FILENAME`` | file name |
201+
+--------------------+------------------------+--------------------------+
202+
| ``'module'`` | N/A | file name |
203+
+--------------------+------------------------+--------------------------+
204+
| ``'ncalls'`` | N/A | call count |
205+
+--------------------+------------------------+--------------------------+
206+
| ``'pcalls'`` | ``SortKey.PCALLS`` | primitive call count |
207+
+--------------------+------------------------+--------------------------+
208+
| ``'line'`` | ``SortKey.LINE`` | line number |
209+
+--------------------+------------------------+--------------------------+
210+
| ``'name'`` | ``SortKey.NAME`` | function name |
211+
+--------------------+------------------------+--------------------------+
212+
| ``'nfl'`` | ``SortKey.NFL`` | name/file/line |
213+
+--------------------+------------------------+--------------------------+
214+
| ``'stdname'`` | ``SortKey.STDNAME`` | standard name |
215+
+--------------------+------------------------+--------------------------+
216+
| ``'time'`` | ``SortKey.TIME`` | internal time |
217+
+--------------------+------------------------+--------------------------+
218+
| ``'tottime'`` | N/A | internal time |
219+
+--------------------+------------------------+--------------------------+
220+
| ``'cumpercall'`` | N/A | cumulative time per call |
221+
+--------------------+------------------------+--------------------------+
222+
| ``'totalpercall'`` | N/A | total time per call |
223+
+--------------------+------------------------+--------------------------+
220224

221225
All sorts on statistics are in descending order (most time consuming
222226
first), while name, file, and line number sorts are ascending
@@ -234,6 +238,9 @@ The :class:`!Stats` class
234238
.. versionadded:: 3.7
235239
The :class:`SortKey` enum.
236240

241+
.. versionadded:: 3.15
242+
Added the ``cumpercall`` and ``totalpercall`` keys.
243+
237244
.. method:: reverse_order()
238245

239246
Reverse the current sort order.

Lib/pstats.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,21 @@ def dump_stats(self, filename):
204204
# list the tuple indices and directions for sorting,
205205
# along with some printable description
206206
sort_arg_dict_default = {
207-
"calls" : (((1,-1), ), "call count"),
208-
"ncalls" : (((1,-1), ), "call count"),
209-
"cumtime" : (((3,-1), ), "cumulative time"),
210-
"cumulative": (((3,-1), ), "cumulative time"),
211-
"filename" : (((4, 1), ), "file name"),
212-
"line" : (((5, 1), ), "line number"),
213-
"module" : (((4, 1), ), "file name"),
214-
"name" : (((6, 1), ), "function name"),
215-
"nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
216-
"pcalls" : (((0,-1), ), "primitive call count"),
217-
"stdname" : (((7, 1), ), "standard name"),
218-
"time" : (((2,-1), ), "internal time"),
219-
"tottime" : (((2,-1), ), "internal time"),
207+
"calls" : (((1,-1), ), "call count"),
208+
"ncalls" : (((1,-1), ), "call count"),
209+
"cumtime" : (((4,-1), ), "cumulative time"),
210+
"cumulative" : (((4,-1), ), "cumulative time"),
211+
"filename" : (((6, 1), ), "file name"),
212+
"line" : (((7, 1), ), "line number"),
213+
"module" : (((6, 1), ), "file name"),
214+
"name" : (((8, 1), ), "function name"),
215+
"nfl" : (((8, 1),(6, 1),(7, 1),), "name/file/line"),
216+
"pcalls" : (((0,-1), ), "primitive call count"),
217+
"stdname" : (((9, 1), ), "standard name"),
218+
"time" : (((2,-1), ), "internal time"),
219+
"tottime" : (((2,-1), ), "internal time"),
220+
"cumpercall" : (((5,-1), ), "cumulative time per call"),
221+
"totalpercall": (((3,-1), ), "total time per call"),
220222
}
221223

222224
def get_sort_arg_defs(self):
@@ -265,8 +267,18 @@ def sort_stats(self, *field):
265267

266268
stats_list = []
267269
for func, (cc, nc, tt, ct, callers) in self.stats.items():
268-
stats_list.append((cc, nc, tt, ct) + func +
269-
(func_std_string(func), func))
270+
if nc == 0:
271+
npc = 0
272+
else:
273+
npc = float(tt)/nc
274+
275+
if cc == 0:
276+
cpc = 0
277+
else:
278+
cpc = float(ct)/cc
279+
280+
stats_list.append((cc, nc, tt, npc, ct, cpc) + func +
281+
(func_std_string(func), func))
270282

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

Lib/test/test_pstats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_sort_stats_int(self):
8585
def test_sort_stats_string(self):
8686
for sort_name in ['calls', 'ncalls', 'cumtime', 'cumulative',
8787
'filename', 'line', 'module', 'name', 'nfl', 'pcalls',
88-
'stdname', 'time', 'tottime']:
88+
'stdname', 'time', 'tottime', 'cumpercall', 'totalpercall']:
8989
self.stats.sort_stats(sort_name)
9090
self.assertEqual(self.stats.sort_type,
9191
self.stats.sort_arg_dict_default[sort_name][-1])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``cumpercall`` and ``totalpercall`` keys to pstats sort_stats

0 commit comments

Comments
 (0)