Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f91ad3

Browse files
committed
Issue #14900: document added sort keys for profile stats.
1 parent 64085e3 commit 5f91ad3

1 file changed

Lines changed: 133 additions & 104 deletions

File tree

Doc/library/profile.rst

Lines changed: 133 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,12 @@ reading the source code for these modules.
240240
43/3 0.533 0.012 0.749 0.250 pobject.py:99(evaluate)
241241
...
242242

243-
The first line indicates that 2706 calls were monitored. Of those calls, 2004
244-
were :dfn:`primitive`. We define :dfn:`primitive` to mean that the call was not
245-
induced via recursion. The next line: ``Ordered by: standard name``, indicates
246-
that the text string in the far right column was used to sort the output. The
247-
column headings include:
243+
The first line indicates that 2706 calls were monitored. Of those
244+
calls, 2004 were :dfn:`primitive`. We define :dfn:`primitive` to
245+
mean that the call was not induced via recursion. The next line:
246+
``Ordered by: standard name``, indicates that the text string in
247+
the far right column was used to sort the output. The column
248+
headings include:
248249

249250
ncalls
250251
for the number of calls,
@@ -266,14 +267,14 @@ reading the source code for these modules.
266267
filename:lineno(function)
267268
provides the respective data of each function
268269

269-
When there are two numbers in the first column (for example, ``43/3``), then the
270-
latter is the number of primitive calls, and the former is the actual number of
271-
calls. Note that when the function does not recurse, these two values are the
272-
same, and only the single figure is printed.
270+
When there are two numbers in the first column (for example,
271+
``43/3``), then the latter is the number of primitive calls, and
272+
the former is the actual number of calls. Note that when the
273+
function does not recurse, these two values are the same, and only
274+
the single figure is printed.
273275

274-
If *sort* is given, it can be one of ``'stdname'`` (sort by filename:lineno),
275-
``'calls'`` (sort by number of calls), ``'time'`` (sort by total time) or
276-
``'cumulative'`` (sort by cumulative time). The default is ``'stdname'``.
276+
If *sort* is given, it can be one of values allowed for *key*
277+
parameter from :meth:`pstats.Stats.sort_stats`.
277278

278279

279280
.. function:: runctx(command, globals, locals, filename=None)
@@ -291,19 +292,22 @@ Analysis of the profiler data is done using the :class:`pstats.Stats` class.
291292

292293
.. class:: Stats(*filenames, stream=sys.stdout)
293294

294-
This class constructor creates an instance of a "statistics object" from a
295-
*filename* (or set of filenames). :class:`Stats` objects are manipulated by
296-
methods, in order to print useful reports. You may specify an alternate output
297-
stream by giving the keyword argument, ``stream``.
298-
299-
The file selected by the above constructor must have been created by the
300-
corresponding version of :mod:`profile` or :mod:`cProfile`. To be specific,
301-
there is *no* file compatibility guaranteed with future versions of this
302-
profiler, and there is no compatibility with files produced by other profilers.
303-
If several files are provided, all the statistics for identical functions will
304-
be coalesced, so that an overall view of several processes can be considered in
305-
a single report. If additional files need to be combined with data in an
306-
existing :class:`Stats` object, the :meth:`add` method can be used.
295+
This class constructor creates an instance of a "statistics object"
296+
from a *filename* (or set of filenames). :class:`Stats` objects
297+
are manipulated by methods, in order to print useful reports. You
298+
may specify an alternate output stream by giving the keyword
299+
argument, ``stream``.
300+
301+
The file selected by the above constructor must have been created
302+
by the corresponding version of :mod:`profile` or :mod:`cProfile`.
303+
To be specific, there is *no* file compatibility guaranteed with
304+
future versions of this profiler, and there is no compatibility
305+
with files produced by other profilers. If several files are
306+
provided, all the statistics for identical functions will be
307+
coalesced, so that an overall view of several processes can be
308+
considered in a single report. If additional files need to be
309+
combined with data in an existing :class:`Stats` object, the
310+
:meth:`add` method can be used.
307311

308312
.. (such as the old system profiler).
309313
@@ -318,15 +322,17 @@ The :class:`Stats` Class
318322

319323
.. method:: Stats.strip_dirs()
320324

321-
This method for the :class:`Stats` class removes all leading path information
322-
from file names. It is very useful in reducing the size of the printout to fit
323-
within (close to) 80 columns. This method modifies the object, and the stripped
324-
information is lost. After performing a strip operation, the object is
325-
considered to have its entries in a "random" order, as it was just after object
326-
initialization and loading. If :meth:`strip_dirs` causes two function names to
327-
be indistinguishable (they are on the same line of the same filename, and have
328-
the same function name), then the statistics for these two entries are
329-
accumulated into a single entry.
325+
This method for the :class:`Stats` class removes all leading path
326+
information from file names. It is very useful in reducing the
327+
size of the printout to fit within (close to) 80 columns. This
328+
method modifies the object, and the stripped information is lost.
329+
After performing a strip operation, the object is considered to
330+
have its entries in a "random" order, as it was just after object
331+
initialization and loading. If :meth:`strip_dirs` causes two
332+
function names to be indistinguishable (they are on the same line
333+
of the same filename, and have the same function name), then the
334+
statistics for these two entries are accumulated into a single
335+
entry.
330336

331337

332338
.. method:: Stats.add(*filenames)
@@ -340,23 +346,25 @@ The :class:`Stats` Class
340346

341347
.. method:: Stats.dump_stats(filename)
342348

343-
Save the data loaded into the :class:`Stats` object to a file named *filename*.
344-
The file is created if it does not exist, and is overwritten if it already
345-
exists. This is equivalent to the method of the same name on the
346-
:class:`profile.Profile` and :class:`cProfile.Profile` classes.
349+
Save the data loaded into the :class:`Stats` object to a file named
350+
*filename*. The file is created if it does not exist, and is
351+
overwritten if it already exists. This is equivalent to the method
352+
of the same name on the :class:`profile.Profile` and
353+
:class:`cProfile.Profile` classes.
347354

348355

349356
.. method:: Stats.sort_stats(*keys)
350357

351-
This method modifies the :class:`Stats` object by sorting it according to the
352-
supplied criteria. The argument is typically a string identifying the basis of
353-
a sort (example: ``'time'`` or ``'name'``).
358+
This method modifies the :class:`Stats` object by sorting it
359+
according to the supplied criteria. The argument is typically a
360+
string identifying the basis of a sort (example: ``'time'`` or
361+
``'name'``).
354362

355-
When more than one key is provided, then additional keys are used as secondary
356-
criteria when there is equality in all keys selected before them. For example,
357-
``sort_stats('name', 'file')`` will sort all the entries according to their
358-
function name, and resolve all ties (identical function names) by sorting by
359-
file name.
363+
When more than one key is provided, then additional keys are used
364+
as secondary criteria when there is equality in all keys selected
365+
before them. For example, ``sort_stats('name', 'file')`` will sort
366+
all the entries according to their function name, and resolve all
367+
ties (identical function names) by sorting by file name.
360368

361369
Abbreviations can be used for any key names, as long as the abbreviation is
362370
unambiguous. The following are the keys currently defined:
@@ -368,10 +376,16 @@ The :class:`Stats` Class
368376
+------------------+----------------------+
369377
| ``'cumulative'`` | cumulative time |
370378
+------------------+----------------------+
379+
| ``'cumtime'`` | cumulative time |
380+
+------------------+----------------------+
371381
| ``'file'`` | file name |
372382
+------------------+----------------------+
383+
| ``'filename'`` | file name |
384+
+------------------+----------------------+
373385
| ``'module'`` | file name |
374386
+------------------+----------------------+
387+
| ``'ncalls'`` | call count |
388+
+------------------+----------------------+
375389
| ``'pcalls'`` | primitive call count |
376390
+------------------+----------------------+
377391
| ``'line'`` | line number |
@@ -384,51 +398,60 @@ The :class:`Stats` Class
384398
+------------------+----------------------+
385399
| ``'time'`` | internal time |
386400
+------------------+----------------------+
401+
| ``'tottime'`` | internal time |
402+
+------------------+----------------------+
387403

388-
Note that all sorts on statistics are in descending order (placing most time
389-
consuming items first), where as name, file, and line number searches are in
390-
ascending order (alphabetical). The subtle distinction between ``'nfl'`` and
391-
``'stdname'`` is that the standard name is a sort of the name as printed, which
392-
means that the embedded line numbers get compared in an odd way. For example,
393-
lines 3, 20, and 40 would (if the file names were the same) appear in the string
394-
order 20, 3 and 40. In contrast, ``'nfl'`` does a numeric compare of the line
395-
numbers. In fact, ``sort_stats('nfl')`` is the same as ``sort_stats('name',
396-
'file', 'line')``.
397-
398-
For backward-compatibility reasons, the numeric arguments ``-1``, ``0``, ``1``,
399-
and ``2`` are permitted. They are interpreted as ``'stdname'``, ``'calls'``,
400-
``'time'``, and ``'cumulative'`` respectively. If this old style format
401-
(numeric) is used, only one sort key (the numeric key) will be used, and
402-
additional arguments will be silently ignored.
404+
Note that all sorts on statistics are in descending order (placing
405+
most time consuming items first), where as name, file, and line
406+
number searches are in ascending order (alphabetical). The subtle
407+
distinction between ``'nfl'`` and ``'stdname'`` is that the
408+
standard name is a sort of the name as printed, which means that
409+
the embedded line numbers get compared in an odd way. For example,
410+
lines 3, 20, and 40 would (if the file names were the same) appear
411+
in the string order 20, 3 and 40. In contrast, ``'nfl'`` does a
412+
numeric compare of the line numbers. In fact,
413+
``sort_stats('nfl')`` is the same as ``sort_stats('name', 'file',
414+
'line')``.
415+
416+
For backward-compatibility reasons, the numeric arguments ``-1``,
417+
``0``, ``1``, and ``2`` are permitted. They are interpreted as
418+
``'stdname'``, ``'calls'``, ``'time'``, and ``'cumulative'``
419+
respectively. If this old style format (numeric) is used, only one
420+
sort key (the numeric key) will be used, and additional arguments
421+
will be silently ignored.
403422

404423
.. For compatibility with the old profiler,
405424
406425
407426
.. method:: Stats.reverse_order()
408427

409-
This method for the :class:`Stats` class reverses the ordering of the basic list
410-
within the object. Note that by default ascending vs descending order is
411-
properly selected based on the sort key of choice.
428+
This method for the :class:`Stats` class reverses the ordering of
429+
the basic list within the object. Note that by default ascending
430+
vs descending order is properly selected based on the sort key of
431+
choice.
412432

413433
.. This method is provided primarily for compatibility with the old profiler.
414434
415435
416436
.. method:: Stats.print_stats(*restrictions)
417437

418-
This method for the :class:`Stats` class prints out a report as described in the
419-
:func:`profile.run` definition.
438+
This method for the :class:`Stats` class prints out a report as
439+
described in the :func:`profile.run` definition.
420440

421-
The order of the printing is based on the last :meth:`sort_stats` operation done
422-
on the object (subject to caveats in :meth:`add` and :meth:`strip_dirs`).
441+
The order of the printing is based on the last :meth:`sort_stats`
442+
operation done on the object (subject to caveats in :meth:`add` and
443+
:meth:`strip_dirs`).
423444

424-
The arguments provided (if any) can be used to limit the list down to the
425-
significant entries. Initially, the list is taken to be the complete set of
426-
profiled functions. Each restriction is either an integer (to select a count of
427-
lines), or a decimal fraction between 0.0 and 1.0 inclusive (to select a
428-
percentage of lines), or a regular expression (to pattern match the standard
429-
name that is printed; as of Python 1.5b1, this uses the Perl-style regular
430-
expression syntax defined by the :mod:`re` module). If several restrictions are
431-
provided, then they are applied sequentially. For example::
445+
The arguments provided (if any) can be used to limit the list down
446+
to the significant entries. Initially, the list is taken to be the
447+
complete set of profiled functions. Each restriction is either an
448+
integer (to select a count of lines), or a decimal fraction between
449+
0.0 and 1.0 inclusive (to select a percentage of lines), or a
450+
regular expression (to pattern match the standard name that is
451+
printed; as of Python 1.5b1, this uses the Perl-style regular
452+
expression syntax defined by the :mod:`re` module). If several
453+
restrictions are provided, then they are applied sequentially. For
454+
example::
432455

433456
print_stats(.1, 'foo:')
434457

@@ -455,17 +478,19 @@ The :class:`Stats` Class
455478
non-parenthesized number repeats the cumulative time spent in the function
456479
at the right.
457480

458-
* With :mod:`cProfile`, each caller is preceded by three numbers: the number of
459-
times this specific call was made, and the total and cumulative times spent in
460-
the current function while it was invoked by this specific caller.
481+
* With :mod:`cProfile`, each caller is preceded by three numbers:
482+
the number of times this specific call was made, and the total
483+
and cumulative times spent in the current function while it was
484+
invoked by this specific caller.
461485

462486

463487
.. method:: Stats.print_callees(*restrictions)
464488

465-
This method for the :class:`Stats` class prints a list of all function that were
466-
called by the indicated function. Aside from this reversal of direction of
467-
calls (re: called vs was called by), the arguments and ordering are identical to
468-
the :meth:`print_callers` method.
489+
This method for the :class:`Stats` class prints a list of all
490+
function that were called by the indicated function. Aside from
491+
this reversal of direction of calls (re: called vs was called by),
492+
the arguments and ordering are identical to the
493+
:meth:`print_callers` method.
469494

470495

471496
.. _profile-limits:
@@ -566,29 +591,33 @@ you want to the :class:`Profile` class constructor::
566591
The resulting profiler will then call :func:`your_time_func`.
567592

568593
:class:`profile.Profile`
569-
:func:`your_time_func` should return a single number, or a list of numbers whose
570-
sum is the current time (like what :func:`os.times` returns). If the function
571-
returns a single time number, or the list of returned numbers has length 2, then
572-
you will get an especially fast version of the dispatch routine.
573-
574-
Be warned that you should calibrate the profiler class for the timer function
575-
that you choose. For most machines, a timer that returns a lone integer value
576-
will provide the best results in terms of low overhead during profiling.
577-
(:func:`os.times` is *pretty* bad, as it returns a tuple of floating point
578-
values). If you want to substitute a better timer in the cleanest fashion,
579-
derive a class and hardwire a replacement dispatch method that best handles your
580-
timer call, along with the appropriate calibration constant.
594+
:func:`your_time_func` should return a single number, or a list of
595+
numbers whose sum is the current time (like what :func:`os.times`
596+
returns). If the function returns a single time number, or the
597+
list of returned numbers has length 2, then you will get an
598+
especially fast version of the dispatch routine.
599+
600+
Be warned that you should calibrate the profiler class for the
601+
timer function that you choose. For most machines, a timer that
602+
returns a lone integer value will provide the best results in terms
603+
of low overhead during profiling. (:func:`os.times` is *pretty*
604+
bad, as it returns a tuple of floating point values). If you want
605+
to substitute a better timer in the cleanest fashion, derive a
606+
class and hardwire a replacement dispatch method that best handles
607+
your timer call, along with the appropriate calibration constant.
581608

582609
:class:`cProfile.Profile`
583-
:func:`your_time_func` should return a single number. If it returns
584-
integers, you can also invoke the class constructor with a second argument
585-
specifying the real duration of one unit of time. For example, if
586-
:func:`your_integer_time_func` returns times measured in thousands of seconds,
587-
you would construct the :class:`Profile` instance as follows::
610+
:func:`your_time_func` should return a single number. If it
611+
returns integers, you can also invoke the class constructor with a
612+
second argument specifying the real duration of one unit of time.
613+
For example, if :func:`your_integer_time_func` returns times
614+
measured in thousands of seconds, you would construct the
615+
:class:`Profile` instance as follows::
588616

589617
pr = profile.Profile(your_integer_time_func, 0.001)
590618

591-
As the :mod:`cProfile.Profile` class cannot be calibrated, custom timer
592-
functions should be used with care and should be as fast as possible. For the
593-
best results with a custom timer, it might be necessary to hard-code it in the C
594-
source of the internal :mod:`_lsprof` module.
619+
As the :mod:`cProfile.Profile` class cannot be calibrated, custom
620+
timer functions should be used with care and should be as fast as
621+
possible. For the best results with a custom timer, it might be
622+
necessary to hard-code it in the C source of the internal
623+
:mod:`_lsprof` module.

0 commit comments

Comments
 (0)