@@ -139,6 +139,7 @@ The :mod:`pstats` module's :class:`~pstats.Stats` class has a variety of methods
139
139
for manipulating and printing the data saved into a profile results file::
140
140
141
141
import pstats
142
+ from pstats import SortKey
142
143
p = pstats.Stats('restats')
143
144
p.strip_dirs().sort_stats(-1).print_stats()
144
145
@@ -148,14 +149,14 @@ entries according to the standard module/line/name string that is printed. The
148
149
:meth: `~pstats.Stats.print_stats ` method printed out all the statistics. You
149
150
might try the following sort calls::
150
151
151
- p.sort_stats('name' )
152
+ p.sort_stats(SortKey.NAME )
152
153
p.print_stats()
153
154
154
155
The first call will actually sort the list by function name, and the second call
155
156
will print out the statistics. The following are some interesting calls to
156
157
experiment with::
157
158
158
- p.sort_stats('cumulative' ).print_stats(10)
159
+ p.sort_stats(SortKey.CUMULATIVE ).print_stats(10)
159
160
160
161
This sorts the profile by cumulative time in a function, and then only prints
161
162
the ten most significant lines. If you want to understand what algorithms are
@@ -164,20 +165,20 @@ taking time, the above line is what you would use.
164
165
If you were looking to see what functions were looping a lot, and taking a lot
165
166
of time, you would do::
166
167
167
- p.sort_stats('time' ).print_stats(10)
168
+ p.sort_stats(SortKey.TIME ).print_stats(10)
168
169
169
170
to sort according to time spent within each function, and then print the
170
171
statistics for the top ten functions.
171
172
172
173
You might also try::
173
174
174
- p.sort_stats('file' ).print_stats('__init__')
175
+ p.sort_stats(SortKey.FILENAME ).print_stats('__init__')
175
176
176
177
This will sort all the statistics by file name, and then print out statistics
177
178
for only the class init methods (since they are spelled with ``__init__ `` in
178
179
them). As one final example, you could try::
179
180
180
- p.sort_stats('time', 'cumulative' ).print_stats(.5, 'init')
181
+ p.sort_stats(SortKey.TIME, SortKey.CUMULATIVE ).print_stats(.5, 'init')
181
182
182
183
This line sorts statistics with a primary key of time, and a secondary key of
183
184
cumulative time, and then prints out some of the statistics. To be specific, the
@@ -250,12 +251,13 @@ functions:
250
251
without writing the profile data to a file::
251
252
252
253
import cProfile, pstats, io
254
+ from pstats import SortKey
253
255
pr = cProfile.Profile()
254
256
pr.enable()
255
257
# ... do something ...
256
258
pr.disable()
257
259
s = io.StringIO()
258
- sortby = 'cumulative'
260
+ sortby = SortKey.CUMULATIVE
259
261
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
260
262
ps.print_stats()
261
263
print(s.getvalue())
@@ -361,60 +363,65 @@ Analysis of the profiler data is done using the :class:`~pstats.Stats` class.
361
363
.. method :: sort_stats(*keys)
362
364
363
365
This method modifies the :class: `Stats ` object by sorting it according to
364
- the supplied criteria. The argument is typically a string identifying the
365
- basis of a sort (example: ``'time' `` or ``'name' ``).
366
+ the supplied criteria. The argument can be either a string or a SortKey
367
+ enum identifying the basis of a sort (example: ``'time' ``, ``'name' ``,
368
+ ``SortKey.TIME `` or ``SortKey.NAME ``). The SortKey enums argument have
369
+ advantage over the string argument in that it is more robust and less
370
+ error prone.
366
371
367
372
When more than one key is provided, then additional keys are used as
368
373
secondary criteria when there is equality in all keys selected before
369
- them. For example, ``sort_stats('name', 'file') `` will sort all the
370
- entries according to their function name, and resolve all ties (identical
371
- function names) by sorting by file name.
372
-
373
- Abbreviations can be used for any key names, as long as the abbreviation
374
- is unambiguous. The following are the keys currently defined:
375
-
376
- +------------------+----------------------+
377
- | Valid Arg | Meaning |
378
- +==================+======================+
379
- | ``'calls' `` | call count |
380
- +------------------+----------------------+
381
- | ``'cumulative' `` | cumulative time |
382
- +------------------+----------------------+
383
- | ``'cumtime' `` | cumulative time |
384
- +------------------+----------------------+
385
- | ``'file' `` | file name |
386
- +------------------+----------------------+
387
- | ``'filename' `` | file name |
388
- +------------------+----------------------+
389
- | ``'module' `` | file name |
390
- +------------------+----------------------+
391
- | ``'ncalls' `` | call count |
392
- +------------------+----------------------+
393
- | ``'pcalls' `` | primitive call count |
394
- +------------------+----------------------+
395
- | ``'line' `` | line number |
396
- +------------------+----------------------+
397
- | ``'name' `` | function name |
398
- +------------------+----------------------+
399
- | ``'nfl' `` | name/file/line |
400
- +------------------+----------------------+
401
- | ``'stdname' `` | standard name |
402
- +------------------+----------------------+
403
- | ``'time' `` | internal time |
404
- +------------------+----------------------+
405
- | ``'tottime' `` | internal time |
406
- +------------------+----------------------+
374
+ them. For example, ``sort_stats(SortKey.NAME, SortKey.FILE) `` will sort
375
+ all the entries according to their function name, and resolve all ties
376
+ (identical function names) by sorting by file name.
377
+
378
+ For the string argument, abbreviations can be used for any key names, as
379
+ long as the abbreviation is unambiguous.
380
+
381
+ The following are the valid string and SortKey:
382
+
383
+ +------------------+---------------------+----------------------+
384
+ | Valid String Arg | Valid enum Arg | Meaning |
385
+ +==================+=====================+======================+
386
+ | ``'calls' `` | SortKey.CALLS | call count |
387
+ +------------------+---------------------+----------------------+
388
+ | ``'cumulative' `` | SortKey.CUMULATIVE | cumulative time |
389
+ +------------------+---------------------+----------------------+
390
+ | ``'cumtime' `` | N/A | cumulative time |
391
+ +------------------+---------------------+----------------------+
392
+ | ``'file' `` | N/A | file name |
393
+ +------------------+---------------------+----------------------+
394
+ | ``'filename' `` | SortKey.FILENAME | file name |
395
+ +------------------+---------------------+----------------------+
396
+ | ``'module' `` | N/A | file name |
397
+ +------------------+---------------------+----------------------+
398
+ | ``'ncalls' `` | N/A | call count |
399
+ +------------------+---------------------+----------------------+
400
+ | ``'pcalls' `` | SortKey.PCALLS | primitive call count |
401
+ +------------------+---------------------+----------------------+
402
+ | ``'line' `` | SortKey.LINE | line number |
403
+ +------------------+---------------------+----------------------+
404
+ | ``'name' `` | SortKey.NAME | function name |
405
+ +------------------+---------------------+----------------------+
406
+ | ``'nfl' `` | SortKey.NFL | name/file/line |
407
+ +------------------+---------------------+----------------------+
408
+ | ``'stdname' `` | SortKey.STDNAME | standard name |
409
+ +------------------+---------------------+----------------------+
410
+ | ``'time' `` | SortKey.TIME | internal time |
411
+ +------------------+---------------------+----------------------+
412
+ | ``'tottime' `` | N/A | internal time |
413
+ +------------------+---------------------+----------------------+
407
414
408
415
Note that all sorts on statistics are in descending order (placing most
409
416
time consuming items first), where as name, file, and line number searches
410
417
are in ascending order (alphabetical). The subtle distinction between
411
- ``'nfl' `` and ``'stdname' `` is that the standard name is a sort of the
412
- name as printed, which means that the embedded line numbers get compared
413
- in an odd way. For example, lines 3, 20, and 40 would (if the file names
414
- were the same) appear in the string order 20, 3 and 40. In contrast,
415
- `` 'nfl' `` does a numeric compare of the line numbers. In fact,
416
- ``sort_stats('nfl' ) `` is the same as `` sort_stats('name', 'file',
417
- 'line' ) ``.
418
+ ``SortKey.NFL `` and ``SortKey.STDNAME `` is that the standard name is a
419
+ sort of the name as printed, which means that the embedded line numbers
420
+ get compared in an odd way. For example, lines 3, 20, and 40 would (if
421
+ the file names were the same) appear in the string order 20, 3 and 40.
422
+ In contrast, `` SortKey.NFL `` does a numeric compare of the line numbers.
423
+ In fact, ``sort_stats(SortKey.NFL ) `` is the same as
424
+ `` sort_stats(SortKey.NAME, SortKey.FILENAME, SortKey.LINE ) ``.
418
425
419
426
For backward-compatibility reasons, the numeric arguments ``-1 ``, ``0 ``,
420
427
``1 ``, and ``2 `` are permitted. They are interpreted as ``'stdname' ``,
@@ -424,6 +431,8 @@ Analysis of the profiler data is done using the :class:`~pstats.Stats` class.
424
431
425
432
.. For compatibility with the old profiler.
426
433
434
+ .. versionadded :: 3.7
435
+ Added the SortKey enum.
427
436
428
437
.. method :: reverse_order()
429
438
0 commit comments