2007-08-15 11:28:22 -03:00
|
|
|
.. _profile:
|
|
|
|
|
|
|
|
********************
|
|
|
|
The Python Profilers
|
|
|
|
********************
|
|
|
|
|
|
|
|
.. sectionauthor:: James Roskind
|
|
|
|
|
Merged revisions 75149,75260-75263,75265-75267,75292,75300,75376,75405,75429-75433,75437,75445,75501,75551,75572,75589-75591,75657,75742,75868,75952-75957,76057,76105,76139,76143,76162,76223 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r75149 | gregory.p.smith | 2009-09-29 16:56:31 -0500 (Tue, 29 Sep 2009) | 3 lines
Mention issue6972 in extractall docs about overwriting things outside of
the supplied path.
........
r75260 | andrew.kuchling | 2009-10-05 16:24:20 -0500 (Mon, 05 Oct 2009) | 1 line
Wording fix
........
r75261 | andrew.kuchling | 2009-10-05 16:24:35 -0500 (Mon, 05 Oct 2009) | 1 line
Fix narkup
........
r75262 | andrew.kuchling | 2009-10-05 16:25:03 -0500 (Mon, 05 Oct 2009) | 1 line
Document 'skip' parameter to constructor
........
r75263 | andrew.kuchling | 2009-10-05 16:25:35 -0500 (Mon, 05 Oct 2009) | 1 line
Note side benefit of socket.create_connection()
........
r75265 | andrew.kuchling | 2009-10-05 17:31:11 -0500 (Mon, 05 Oct 2009) | 1 line
Reword sentence
........
r75266 | andrew.kuchling | 2009-10-05 17:32:48 -0500 (Mon, 05 Oct 2009) | 1 line
Use standard comma punctuation; reword some sentences in the docs
........
r75267 | andrew.kuchling | 2009-10-05 17:42:56 -0500 (Mon, 05 Oct 2009) | 1 line
Backport r73983: Document the thousands separator.
........
r75292 | benjamin.peterson | 2009-10-08 22:11:36 -0500 (Thu, 08 Oct 2009) | 1 line
death to old CVS keyword
........
r75300 | benjamin.peterson | 2009-10-09 16:48:14 -0500 (Fri, 09 Oct 2009) | 1 line
fix some coding style
........
r75376 | benjamin.peterson | 2009-10-11 20:26:07 -0500 (Sun, 11 Oct 2009) | 1 line
platform we don't care about
........
r75405 | neil.schemenauer | 2009-10-14 12:17:14 -0500 (Wed, 14 Oct 2009) | 4 lines
Issue #1754094: Improve the stack depth calculation in the compiler.
There should be no other effect than a small decrease in memory use.
Patch by Christopher Tur Lesniewski-Laas.
........
r75429 | benjamin.peterson | 2009-10-14 20:47:28 -0500 (Wed, 14 Oct 2009) | 1 line
pep8ify if blocks
........
r75430 | benjamin.peterson | 2009-10-14 20:49:37 -0500 (Wed, 14 Oct 2009) | 1 line
use floor division and add a test that exercises the tabsize codepath
........
r75431 | benjamin.peterson | 2009-10-14 20:56:25 -0500 (Wed, 14 Oct 2009) | 1 line
change test to what I intended
........
r75432 | benjamin.peterson | 2009-10-14 22:05:39 -0500 (Wed, 14 Oct 2009) | 1 line
some cleanups
........
r75433 | benjamin.peterson | 2009-10-14 22:06:55 -0500 (Wed, 14 Oct 2009) | 1 line
make inspect.isabstract() always return a boolean; add a test for it, too #7069
........
r75437 | benjamin.peterson | 2009-10-15 10:44:46 -0500 (Thu, 15 Oct 2009) | 1 line
only clear a module's __dict__ if the module is the only one with a reference to it #7140
........
r75445 | vinay.sajip | 2009-10-16 09:06:44 -0500 (Fri, 16 Oct 2009) | 1 line
Issue #7120: logging: Removed import of multiprocessing which is causing crash in GAE.
........
r75501 | antoine.pitrou | 2009-10-18 13:37:11 -0500 (Sun, 18 Oct 2009) | 3 lines
Add a comment about unreachable code, and fix a typo
........
r75551 | benjamin.peterson | 2009-10-19 22:14:10 -0500 (Mon, 19 Oct 2009) | 1 line
use property api
........
r75572 | benjamin.peterson | 2009-10-20 16:55:17 -0500 (Tue, 20 Oct 2009) | 1 line
clarify buffer arg #7178
........
r75589 | benjamin.peterson | 2009-10-21 21:26:47 -0500 (Wed, 21 Oct 2009) | 1 line
whitespace
........
r75590 | benjamin.peterson | 2009-10-21 21:36:47 -0500 (Wed, 21 Oct 2009) | 1 line
rewrite to be nice to other implementations
........
r75591 | benjamin.peterson | 2009-10-21 21:50:38 -0500 (Wed, 21 Oct 2009) | 4 lines
rewrite for style, clarify, and comments
Also, use the hasattr() like scheme of allowing BaseException exceptions through.
........
r75657 | antoine.pitrou | 2009-10-24 07:41:27 -0500 (Sat, 24 Oct 2009) | 3 lines
Fix compilation error in debug mode.
........
r75742 | benjamin.peterson | 2009-10-26 17:51:16 -0500 (Mon, 26 Oct 2009) | 1 line
use 'is' instead of id()
........
r75868 | benjamin.peterson | 2009-10-27 15:59:18 -0500 (Tue, 27 Oct 2009) | 1 line
test expect base classes
........
r75952 | georg.brandl | 2009-10-29 15:38:32 -0500 (Thu, 29 Oct 2009) | 1 line
Use the correct function name in docstring.
........
r75953 | georg.brandl | 2009-10-29 15:39:50 -0500 (Thu, 29 Oct 2009) | 1 line
Remove mention of the old -X command line switch.
........
r75954 | georg.brandl | 2009-10-29 15:53:00 -0500 (Thu, 29 Oct 2009) | 1 line
Use constants instead of magic integers for test result. Do not re-run with --verbose3 for environment changing tests.
........
r75955 | georg.brandl | 2009-10-29 15:54:03 -0500 (Thu, 29 Oct 2009) | 1 line
Use a single style for all the docstrings in the math module.
........
r75956 | georg.brandl | 2009-10-29 16:16:34 -0500 (Thu, 29 Oct 2009) | 1 line
I do not think the "railroad" program mentioned is still available.
........
r75957 | georg.brandl | 2009-10-29 16:44:56 -0500 (Thu, 29 Oct 2009) | 1 line
Fix constant name.
........
r76057 | benjamin.peterson | 2009-11-02 09:06:45 -0600 (Mon, 02 Nov 2009) | 1 line
prevent a rather unlikely segfault
........
r76105 | georg.brandl | 2009-11-04 01:38:12 -0600 (Wed, 04 Nov 2009) | 1 line
#7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
........
r76139 | benjamin.peterson | 2009-11-06 19:04:38 -0600 (Fri, 06 Nov 2009) | 1 line
spelling
........
r76143 | georg.brandl | 2009-11-07 02:26:07 -0600 (Sat, 07 Nov 2009) | 1 line
#7271: fix typo.
........
r76162 | benjamin.peterson | 2009-11-08 22:10:53 -0600 (Sun, 08 Nov 2009) | 1 line
discuss how to use -p
........
r76223 | georg.brandl | 2009-11-12 02:29:46 -0600 (Thu, 12 Nov 2009) | 1 line
Give the profile module a module directive.
........
2009-11-12 22:25:08 -04:00
|
|
|
.. module:: profile
|
|
|
|
:synopsis: Python source profiler.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
**Source code:** :source:`Lib/profile.py` and :source:`Lib/pstats.py`
|
|
|
|
|
|
|
|
--------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. _profiler-introduction:
|
|
|
|
|
|
|
|
Introduction to the profilers
|
|
|
|
=============================
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: deterministic profiling
|
|
|
|
single: profiling, deterministic
|
|
|
|
|
2010-10-14 03:41:42 -03:00
|
|
|
A :dfn:`profiler` is a program that describes the run time performance of a
|
|
|
|
program, providing a variety of statistics. This documentation describes the
|
|
|
|
profiler functionality provided in the modules :mod:`cProfile`, :mod:`profile`
|
|
|
|
and :mod:`pstats`. This profiler provides :dfn:`deterministic profiling` of
|
|
|
|
Python programs. It also provides a series of report generation tools to allow
|
|
|
|
users to rapidly examine the results of a profile operation.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2007-10-11 15:01:43 -03:00
|
|
|
The Python standard library provides two different profilers:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-10-14 03:41:42 -03:00
|
|
|
1. :mod:`cProfile` is recommended for most users; it's a C extension with
|
|
|
|
reasonable overhead that makes it suitable for profiling long-running
|
|
|
|
programs. Based on :mod:`lsprof`, contributed by Brett Rosen and Ted
|
|
|
|
Czotter.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-10-14 03:41:42 -03:00
|
|
|
2. :mod:`profile`, a pure Python module whose interface is imitated by
|
|
|
|
:mod:`cProfile`. Adds significant overhead to profiled programs. If you're
|
|
|
|
trying to extend the profiler in some way, the task might be easier with this
|
2011-07-28 17:56:24 -03:00
|
|
|
module.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The :mod:`profile` and :mod:`cProfile` modules export the same interface, so
|
Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62350 | nick.coghlan | 2008-04-15 12:25:31 +0200 (Tue, 15 Apr 2008) | 1 line
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
........
r62351 | nick.coghlan | 2008-04-15 12:28:14 +0200 (Tue, 15 Apr 2008) | 1 line
Add test file missing from rev 62350
........
r62352 | benjamin.peterson | 2008-04-15 13:58:46 +0200 (Tue, 15 Apr 2008) | 2 lines
Add myself to Doc/ACKS.txt
........
r62353 | andrew.kuchling | 2008-04-15 15:10:07 +0200 (Tue, 15 Apr 2008) | 6 lines
Add *,**,@ to index, as suggested by
http://farmdev.com/thoughts/24/what-does-the-def-star-variable-or-def-asterisk-parameter-syntax-do-in-python-/
The right entry type to use isn't clear; operator seems wrong, because *,**,@
aren't being used in expressions here. I put them as 'statement'; 'syntax'
might be better.
........
r62354 | andrew.kuchling | 2008-04-15 15:10:41 +0200 (Tue, 15 Apr 2008) | 1 line
Typo fix
........
r62355 | mark.dickinson | 2008-04-15 22:51:18 +0200 (Tue, 15 Apr 2008) | 3 lines
Fix for possible signed overflow: the behaviour of -LONG_MIN is
undefined in ANSI C.
........
r62358 | jeroen.ruigrok | 2008-04-16 14:47:01 +0200 (Wed, 16 Apr 2008) | 2 lines
Reformat to 80 columns prior to adding documentation.
........
r62359 | jeroen.ruigrok | 2008-04-16 14:57:43 +0200 (Wed, 16 Apr 2008) | 2 lines
Add details about the return value for mmap.flush().
........
r62364 | raymond.hettinger | 2008-04-17 12:48:31 +0200 (Thu, 17 Apr 2008) | 1 line
Issue 2648: Add leading zero to money format recipe in the docs.
........
r62365 | jeroen.ruigrok | 2008-04-17 14:39:45 +0200 (Thu, 17 Apr 2008) | 2 lines
Be consistent in the use of read-only.
........
r62370 | andrew.kuchling | 2008-04-17 22:44:06 +0200 (Thu, 17 Apr 2008) | 1 line
Typo fixes
........
r62372 | andrew.kuchling | 2008-04-18 04:40:47 +0200 (Fri, 18 Apr 2008) | 1 line
Use correct parameter name
........
r62373 | andrew.kuchling | 2008-04-18 18:53:09 +0200 (Fri, 18 Apr 2008) | 1 line
#2654: fix typo
........
r62374 | andrew.kuchling | 2008-04-18 20:28:23 +0200 (Fri, 18 Apr 2008) | 4 lines
Remove personal note from Jim Roskind; it no longer applies, and the
e-mail address is for a previous employer.
Can we move the big long copyright statement into a sidebar or something?
........
r62375 | andrew.kuchling | 2008-04-18 20:39:55 +0200 (Fri, 18 Apr 2008) | 1 line
Rewrite introductory section, and remove old section. (It was already commented-out, but why keep it?)
........
r62378 | skip.montanaro | 2008-04-18 22:35:46 +0200 (Fri, 18 Apr 2008) | 1 line
resolve issue 2014
........
r62379 | benjamin.peterson | 2008-04-18 22:45:33 +0200 (Fri, 18 Apr 2008) | 2 lines
Fix indentation in sysmodule.c
........
r62381 | amaury.forgeotdarc | 2008-04-19 01:31:33 +0200 (Sat, 19 Apr 2008) | 3 lines
Some tests did not pass on repeated calls (regrtest -R::)
Perform additional cleanup, mostly deleting from sys.modules, or clearing the warnings registry.
........
2008-04-18 21:55:37 -03:00
|
|
|
they are mostly interchangeable; :mod:`cProfile` has a much lower overhead but
|
2010-10-14 03:41:42 -03:00
|
|
|
is newer and might not be available on all systems. :mod:`cProfile` is really a
|
|
|
|
compatibility layer on top of the internal :mod:`_lsprof` module.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The profiler modules are designed to provide an execution profile for a given
|
|
|
|
program, not for benchmarking purposes (for that, there is :mod:`timeit` for
|
2011-04-03 10:20:21 -03:00
|
|
|
reasonably accurate results). This particularly applies to benchmarking
|
2010-10-14 03:41:42 -03:00
|
|
|
Python code against C code: the profilers introduce overhead for Python code,
|
|
|
|
but not for C-level functions, and so the C code would seem faster than any
|
|
|
|
Python one.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _profile-instant:
|
|
|
|
|
|
|
|
Instant User's Manual
|
|
|
|
=====================
|
|
|
|
|
|
|
|
This section is provided for users that "don't want to read the manual." It
|
|
|
|
provides a very brief overview, and allows a user to rapidly perform profiling
|
|
|
|
on an existing application.
|
|
|
|
|
|
|
|
To profile an application with a main entry point of :func:`foo`, you would add
|
|
|
|
the following to your module::
|
|
|
|
|
|
|
|
import cProfile
|
|
|
|
cProfile.run('foo()')
|
|
|
|
|
|
|
|
(Use :mod:`profile` instead of :mod:`cProfile` if the latter is not available on
|
|
|
|
your system.)
|
|
|
|
|
|
|
|
The above action would cause :func:`foo` to be run, and a series of informative
|
|
|
|
lines (the profile) to be printed. The above approach is most useful when
|
|
|
|
working with the interpreter. If you would like to save the results of a
|
|
|
|
profile into a file for later examination, you can supply a file name as the
|
|
|
|
second argument to the :func:`run` function::
|
|
|
|
|
|
|
|
import cProfile
|
|
|
|
cProfile.run('foo()', 'fooprof')
|
|
|
|
|
|
|
|
The file :file:`cProfile.py` can also be invoked as a script to profile another
|
|
|
|
script. For example::
|
|
|
|
|
|
|
|
python -m cProfile myscript.py
|
|
|
|
|
|
|
|
:file:`cProfile.py` accepts two optional arguments on the command line::
|
|
|
|
|
|
|
|
cProfile.py [-o output_file] [-s sort_order]
|
|
|
|
|
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77484 | skip.montanaro | 2010-01-13 19:12:34 -0600 (Wed, 13 Jan 2010) | 4 lines
Update PyEval_EvalFrame to PyEval_EvalFrameEx. This looks to have been done
partially before. Also add a comment describing how this might have to work
with different versions of the interpreter.
........
r77487 | ezio.melotti | 2010-01-14 05:34:10 -0600 (Thu, 14 Jan 2010) | 1 line
Fixed typo
........
r77561 | georg.brandl | 2010-01-17 02:42:30 -0600 (Sun, 17 Jan 2010) | 1 line
#7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such.
........
r77570 | georg.brandl | 2010-01-17 06:14:42 -0600 (Sun, 17 Jan 2010) | 1 line
Add note about usage of STRINGLIB_EMPTY.
........
r77593 | georg.brandl | 2010-01-17 17:33:53 -0600 (Sun, 17 Jan 2010) | 1 line
Fix internal reference.
........
r77603 | benjamin.peterson | 2010-01-18 17:07:56 -0600 (Mon, 18 Jan 2010) | 8 lines
data descriptors do not override the class dictionary if __get__ is not defined
Adjust documentation and add a test to verify this behavior.
See http://mail.python.org/pipermail/python-dev/2010-January/095637.html for
discussion.
........
r77608 | gregory.p.smith | 2010-01-19 02:19:03 -0600 (Tue, 19 Jan 2010) | 6 lines
Do not compile stubs for the sha2 series hashes in the openssl hashlib
module when the openssl version is too old to support them. That
leads both compiled code bloat and to unittests attempting to test
implementations that don't exist for comparison purposes on such
platforms.
........
r77667 | mark.dickinson | 2010-01-21 12:32:27 -0600 (Thu, 21 Jan 2010) | 1 line
Add two more test_strtod test values.
........
r77702 | georg.brandl | 2010-01-23 02:43:31 -0600 (Sat, 23 Jan 2010) | 1 line
#7762: fix refcount annotation of PyUnicode_Tailmatch().
........
r77703 | georg.brandl | 2010-01-23 02:47:54 -0600 (Sat, 23 Jan 2010) | 1 line
#7725: fix referencing issue.
........
r77739 | benjamin.peterson | 2010-01-24 21:52:52 -0600 (Sun, 24 Jan 2010) | 1 line
mention from_float() in error message
........
r77858 | georg.brandl | 2010-01-30 11:57:48 -0600 (Sat, 30 Jan 2010) | 1 line
#7802: fix invalid example (heh).
........
r77887 | georg.brandl | 2010-01-31 12:51:49 -0600 (Sun, 31 Jan 2010) | 5 lines
Fix-up ftplib documentation:
move exception descriptions to toplevel, not inside a class
remove attribution in "versionadded"
spell and grammar check docstring of FTP_TLS
........
r77889 | michael.foord | 2010-01-31 13:59:26 -0600 (Sun, 31 Jan 2010) | 1 line
Minor modification to unittest documentation.
........
2010-02-02 22:35:45 -04:00
|
|
|
``-s`` only applies to standard output (``-o`` is not supplied).
|
2007-08-15 11:28:22 -03:00
|
|
|
Look in the :class:`Stats` documentation for valid sort values.
|
|
|
|
|
|
|
|
When you wish to review the profile, you should use the methods in the
|
|
|
|
:mod:`pstats` module. Typically you would load the statistics data as follows::
|
|
|
|
|
|
|
|
import pstats
|
|
|
|
p = pstats.Stats('fooprof')
|
|
|
|
|
|
|
|
The class :class:`Stats` (the above code just created an instance of this class)
|
|
|
|
has a variety of methods for manipulating and printing the data that was just
|
|
|
|
read into ``p``. When you ran :func:`cProfile.run` above, what was printed was
|
|
|
|
the result of three method calls::
|
|
|
|
|
|
|
|
p.strip_dirs().sort_stats(-1).print_stats()
|
|
|
|
|
|
|
|
The first method removed the extraneous path from all the module names. The
|
|
|
|
second method sorted all the entries according to the standard module/line/name
|
|
|
|
string that is printed. The third method printed out all the statistics. You
|
|
|
|
might try the following sort calls:
|
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. (this is to comply with the semantics of the old profiler).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
p.sort_stats('name')
|
|
|
|
p.print_stats()
|
|
|
|
|
|
|
|
The first call will actually sort the list by function name, and the second call
|
|
|
|
will print out the statistics. The following are some interesting calls to
|
|
|
|
experiment with::
|
|
|
|
|
|
|
|
p.sort_stats('cumulative').print_stats(10)
|
|
|
|
|
|
|
|
This sorts the profile by cumulative time in a function, and then only prints
|
|
|
|
the ten most significant lines. If you want to understand what algorithms are
|
|
|
|
taking time, the above line is what you would use.
|
|
|
|
|
|
|
|
If you were looking to see what functions were looping a lot, and taking a lot
|
|
|
|
of time, you would do::
|
|
|
|
|
|
|
|
p.sort_stats('time').print_stats(10)
|
|
|
|
|
|
|
|
to sort according to time spent within each function, and then print the
|
|
|
|
statistics for the top ten functions.
|
|
|
|
|
|
|
|
You might also try::
|
|
|
|
|
|
|
|
p.sort_stats('file').print_stats('__init__')
|
|
|
|
|
|
|
|
This will sort all the statistics by file name, and then print out statistics
|
|
|
|
for only the class init methods (since they are spelled with ``__init__`` in
|
|
|
|
them). As one final example, you could try::
|
|
|
|
|
|
|
|
p.sort_stats('time', 'cum').print_stats(.5, 'init')
|
|
|
|
|
|
|
|
This line sorts statistics with a primary key of time, and a secondary key of
|
|
|
|
cumulative time, and then prints out some of the statistics. To be specific, the
|
|
|
|
list is first culled down to 50% (re: ``.5``) of its original size, then only
|
|
|
|
lines containing ``init`` are maintained, and that sub-sub-list is printed.
|
|
|
|
|
|
|
|
If you wondered what functions called the above functions, you could now (``p``
|
|
|
|
is still sorted according to the last criteria) do::
|
|
|
|
|
|
|
|
p.print_callers(.5, 'init')
|
|
|
|
|
|
|
|
and you would get a list of callers for each of the listed functions.
|
|
|
|
|
|
|
|
If you want more functionality, you're going to have to read the manual, or
|
|
|
|
guess what the following functions do::
|
|
|
|
|
|
|
|
p.print_callees()
|
|
|
|
p.add('fooprof')
|
|
|
|
|
|
|
|
Invoked as a script, the :mod:`pstats` module is a statistics browser for
|
|
|
|
reading and examining profile dumps. It has a simple line-oriented interface
|
|
|
|
(implemented using :mod:`cmd`) and interactive help.
|
|
|
|
|
|
|
|
|
|
|
|
.. _deterministic-profiling:
|
|
|
|
|
|
|
|
What Is Deterministic Profiling?
|
|
|
|
================================
|
|
|
|
|
|
|
|
:dfn:`Deterministic profiling` is meant to reflect the fact that all *function
|
|
|
|
call*, *function return*, and *exception* events are monitored, and precise
|
|
|
|
timings are made for the intervals between these events (during which time the
|
|
|
|
user's code is executing). In contrast, :dfn:`statistical profiling` (which is
|
|
|
|
not done by this module) randomly samples the effective instruction pointer, and
|
|
|
|
deduces where time is being spent. The latter technique traditionally involves
|
|
|
|
less overhead (as the code does not need to be instrumented), but provides only
|
|
|
|
relative indications of where time is being spent.
|
|
|
|
|
|
|
|
In Python, since there is an interpreter active during execution, the presence
|
|
|
|
of instrumented code is not required to do deterministic profiling. Python
|
|
|
|
automatically provides a :dfn:`hook` (optional callback) for each event. In
|
|
|
|
addition, the interpreted nature of Python tends to add so much overhead to
|
|
|
|
execution, that deterministic profiling tends to only add small processing
|
|
|
|
overhead in typical applications. The result is that deterministic profiling is
|
|
|
|
not that expensive, yet provides extensive run time statistics about the
|
|
|
|
execution of a Python program.
|
|
|
|
|
|
|
|
Call count statistics can be used to identify bugs in code (surprising counts),
|
|
|
|
and to identify possible inline-expansion points (high call counts). Internal
|
|
|
|
time statistics can be used to identify "hot loops" that should be carefully
|
|
|
|
optimized. Cumulative time statistics should be used to identify high level
|
|
|
|
errors in the selection of algorithms. Note that the unusual handling of
|
|
|
|
cumulative times in this profiler allows statistics for recursive
|
|
|
|
implementations of algorithms to be directly compared to iterative
|
|
|
|
implementations.
|
|
|
|
|
|
|
|
|
|
|
|
Reference Manual -- :mod:`profile` and :mod:`cProfile`
|
|
|
|
======================================================
|
|
|
|
|
|
|
|
.. module:: cProfile
|
|
|
|
:synopsis: Python profiler
|
|
|
|
|
|
|
|
|
|
|
|
The primary entry point for the profiler is the global function
|
|
|
|
:func:`profile.run` (resp. :func:`cProfile.run`). It is typically used to create
|
|
|
|
any profile information. The reports are formatted and printed using methods of
|
|
|
|
the class :class:`pstats.Stats`. The following is a description of all of these
|
|
|
|
standard entry points and functions. For a more in-depth view of some of the
|
|
|
|
code, consider reading the later section on Profiler Extensions, which includes
|
|
|
|
discussion of how to derive "better" profilers from the classes presented, or
|
|
|
|
reading the source code for these modules.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. function:: run(command, filename=None, sort=-1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This function takes a single argument that can be passed to the :func:`exec`
|
|
|
|
function, and an optional file name. In all cases this routine attempts to
|
|
|
|
:func:`exec` its first argument, and gather profiling statistics from the
|
|
|
|
execution. If no file name is present, then this function automatically
|
|
|
|
prints a simple profiling report, sorted by the standard name string
|
|
|
|
(file/line/function-name) that is presented in each line. The following is a
|
|
|
|
typical output from such a call::
|
|
|
|
|
|
|
|
2706 function calls (2004 primitive calls) in 4.504 CPU seconds
|
|
|
|
|
|
|
|
Ordered by: standard name
|
|
|
|
|
|
|
|
ncalls tottime percall cumtime percall filename:lineno(function)
|
|
|
|
2 0.006 0.003 0.953 0.477 pobject.py:75(save_objects)
|
|
|
|
43/3 0.533 0.012 0.749 0.250 pobject.py:99(evaluate)
|
|
|
|
...
|
|
|
|
|
|
|
|
The first line indicates that 2706 calls were monitored. Of those calls, 2004
|
|
|
|
were :dfn:`primitive`. We define :dfn:`primitive` to mean that the call was not
|
|
|
|
induced via recursion. The next line: ``Ordered by: standard name``, indicates
|
|
|
|
that the text string in the far right column was used to sort the output. The
|
|
|
|
column headings include:
|
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
ncalls
|
2007-08-15 11:28:22 -03:00
|
|
|
for the number of calls,
|
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
tottime
|
2009-09-02 17:34:52 -03:00
|
|
|
for the total time spent in the given function (and excluding time made in
|
|
|
|
calls to sub-functions),
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
percall
|
2007-08-15 11:28:22 -03:00
|
|
|
is the quotient of ``tottime`` divided by ``ncalls``
|
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
cumtime
|
2007-08-15 11:28:22 -03:00
|
|
|
is the total time spent in this and all subfunctions (from invocation till
|
|
|
|
exit). This figure is accurate *even* for recursive functions.
|
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
percall
|
2007-08-15 11:28:22 -03:00
|
|
|
is the quotient of ``cumtime`` divided by primitive calls
|
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
filename:lineno(function)
|
2007-08-15 11:28:22 -03:00
|
|
|
provides the respective data of each function
|
|
|
|
|
|
|
|
When there are two numbers in the first column (for example, ``43/3``), then the
|
|
|
|
latter is the number of primitive calls, and the former is the actual number of
|
|
|
|
calls. Note that when the function does not recurse, these two values are the
|
|
|
|
same, and only the single figure is printed.
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
If *sort* is given, it can be one of ``'stdname'`` (sort by filename:lineno),
|
|
|
|
``'calls'`` (sort by number of calls), ``'time'`` (sort by total time) or
|
|
|
|
``'cumulative'`` (sort by cumulative time). The default is ``'stdname'``.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. function:: runctx(command, globals, locals, filename=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This function is similar to :func:`run`, with added arguments to supply the
|
|
|
|
globals and locals dictionaries for the *command* string.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
Analysis of the profiler data is done using the :class:`pstats.Stats` class.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. module:: pstats
|
|
|
|
:synopsis: Statistics object for use with the profiler.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. class:: Stats(*filenames, stream=sys.stdout)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This class constructor creates an instance of a "statistics object" from a
|
|
|
|
*filename* (or set of filenames). :class:`Stats` objects are manipulated by
|
|
|
|
methods, in order to print useful reports. You may specify an alternate output
|
|
|
|
stream by giving the keyword argument, ``stream``.
|
|
|
|
|
|
|
|
The file selected by the above constructor must have been created by the
|
|
|
|
corresponding version of :mod:`profile` or :mod:`cProfile`. To be specific,
|
|
|
|
there is *no* file compatibility guaranteed with future versions of this
|
|
|
|
profiler, and there is no compatibility with files produced by other profilers.
|
|
|
|
If several files are provided, all the statistics for identical functions will
|
|
|
|
be coalesced, so that an overall view of several processes can be considered in
|
|
|
|
a single report. If additional files need to be combined with data in an
|
|
|
|
existing :class:`Stats` object, the :meth:`add` method can be used.
|
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. (such as the old system profiler).
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. _profile-stats:
|
|
|
|
|
|
|
|
The :class:`Stats` Class
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
:class:`Stats` objects have the following methods:
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Stats.strip_dirs()
|
|
|
|
|
|
|
|
This method for the :class:`Stats` class removes all leading path information
|
|
|
|
from file names. It is very useful in reducing the size of the printout to fit
|
|
|
|
within (close to) 80 columns. This method modifies the object, and the stripped
|
|
|
|
information is lost. After performing a strip operation, the object is
|
|
|
|
considered to have its entries in a "random" order, as it was just after object
|
|
|
|
initialization and loading. If :meth:`strip_dirs` causes two function names to
|
|
|
|
be indistinguishable (they are on the same line of the same filename, and have
|
|
|
|
the same function name), then the statistics for these two entries are
|
|
|
|
accumulated into a single entry.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. method:: Stats.add(*filenames)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This method of the :class:`Stats` class accumulates additional profiling
|
|
|
|
information into the current profiling object. Its arguments should refer to
|
|
|
|
filenames created by the corresponding version of :func:`profile.run` or
|
|
|
|
:func:`cProfile.run`. Statistics for identically named (re: file, line, name)
|
|
|
|
functions are automatically accumulated into single function statistics.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Stats.dump_stats(filename)
|
|
|
|
|
|
|
|
Save the data loaded into the :class:`Stats` object to a file named *filename*.
|
|
|
|
The file is created if it does not exist, and is overwritten if it already
|
|
|
|
exists. This is equivalent to the method of the same name on the
|
|
|
|
:class:`profile.Profile` and :class:`cProfile.Profile` classes.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. method:: Stats.sort_stats(*keys)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This method modifies the :class:`Stats` object by sorting it according to the
|
|
|
|
supplied criteria. The argument is typically a string identifying the basis of
|
|
|
|
a sort (example: ``'time'`` or ``'name'``).
|
|
|
|
|
|
|
|
When more than one key is provided, then additional keys are used as secondary
|
|
|
|
criteria when there is equality in all keys selected before them. For example,
|
|
|
|
``sort_stats('name', 'file')`` will sort all the entries according to their
|
|
|
|
function name, and resolve all ties (identical function names) by sorting by
|
|
|
|
file name.
|
|
|
|
|
|
|
|
Abbreviations can be used for any key names, as long as the abbreviation is
|
|
|
|
unambiguous. The following are the keys currently defined:
|
|
|
|
|
|
|
|
+------------------+----------------------+
|
|
|
|
| Valid Arg | Meaning |
|
|
|
|
+==================+======================+
|
|
|
|
| ``'calls'`` | call count |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'cumulative'`` | cumulative time |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'file'`` | file name |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'module'`` | file name |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'pcalls'`` | primitive call count |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'line'`` | line number |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'name'`` | function name |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'nfl'`` | name/file/line |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'stdname'`` | standard name |
|
|
|
|
+------------------+----------------------+
|
|
|
|
| ``'time'`` | internal time |
|
|
|
|
+------------------+----------------------+
|
|
|
|
|
|
|
|
Note that all sorts on statistics are in descending order (placing most time
|
|
|
|
consuming items first), where as name, file, and line number searches are in
|
|
|
|
ascending order (alphabetical). The subtle distinction between ``'nfl'`` and
|
|
|
|
``'stdname'`` is that the standard name is a sort of the name as printed, which
|
|
|
|
means that the embedded line numbers get compared in an odd way. For example,
|
|
|
|
lines 3, 20, and 40 would (if the file names were the same) appear in the string
|
|
|
|
order 20, 3 and 40. In contrast, ``'nfl'`` does a numeric compare of the line
|
|
|
|
numbers. In fact, ``sort_stats('nfl')`` is the same as ``sort_stats('name',
|
|
|
|
'file', 'line')``.
|
|
|
|
|
|
|
|
For backward-compatibility reasons, the numeric arguments ``-1``, ``0``, ``1``,
|
|
|
|
and ``2`` are permitted. They are interpreted as ``'stdname'``, ``'calls'``,
|
|
|
|
``'time'``, and ``'cumulative'`` respectively. If this old style format
|
|
|
|
(numeric) is used, only one sort key (the numeric key) will be used, and
|
|
|
|
additional arguments will be silently ignored.
|
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. For compatibility with the old profiler,
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: Stats.reverse_order()
|
|
|
|
|
|
|
|
This method for the :class:`Stats` class reverses the ordering of the basic list
|
|
|
|
within the object. Note that by default ascending vs descending order is
|
|
|
|
properly selected based on the sort key of choice.
|
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. This method is provided primarily for compatibility with the old profiler.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. method:: Stats.print_stats(*restrictions)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This method for the :class:`Stats` class prints out a report as described in the
|
|
|
|
:func:`profile.run` definition.
|
|
|
|
|
|
|
|
The order of the printing is based on the last :meth:`sort_stats` operation done
|
|
|
|
on the object (subject to caveats in :meth:`add` and :meth:`strip_dirs`).
|
|
|
|
|
|
|
|
The arguments provided (if any) can be used to limit the list down to the
|
|
|
|
significant entries. Initially, the list is taken to be the complete set of
|
|
|
|
profiled functions. Each restriction is either an integer (to select a count of
|
|
|
|
lines), or a decimal fraction between 0.0 and 1.0 inclusive (to select a
|
|
|
|
percentage of lines), or a regular expression (to pattern match the standard
|
|
|
|
name that is printed; as of Python 1.5b1, this uses the Perl-style regular
|
|
|
|
expression syntax defined by the :mod:`re` module). If several restrictions are
|
|
|
|
provided, then they are applied sequentially. For example::
|
|
|
|
|
|
|
|
print_stats(.1, 'foo:')
|
|
|
|
|
|
|
|
would first limit the printing to first 10% of list, and then only print
|
|
|
|
functions that were part of filename :file:`.\*foo:`. In contrast, the
|
|
|
|
command::
|
|
|
|
|
|
|
|
print_stats('foo:', .1)
|
|
|
|
|
|
|
|
would limit the list to all functions having file names :file:`.\*foo:`, and
|
|
|
|
then proceed to only print the first 10% of them.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. method:: Stats.print_callers(*restrictions)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This method for the :class:`Stats` class prints a list of all functions that
|
|
|
|
called each function in the profiled database. The ordering is identical to
|
|
|
|
that provided by :meth:`print_stats`, and the definition of the restricting
|
|
|
|
argument is also identical. Each caller is reported on its own line. The
|
|
|
|
format differs slightly depending on the profiler that produced the stats:
|
|
|
|
|
|
|
|
* With :mod:`profile`, a number is shown in parentheses after each caller to
|
|
|
|
show how many times this specific call was made. For convenience, a second
|
|
|
|
non-parenthesized number repeats the cumulative time spent in the function
|
|
|
|
at the right.
|
|
|
|
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line
Added PEP 3101.
........
r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines
Fixes contributed by Ori Avtalion.
........
r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line
Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
........
r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line
Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it.
........
r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines
Close manifest file.
This change doesn't make any difference to CPython, but is a necessary fix for Jython.
........
r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines
Remove news about float repr() -- issue 1580 is still in limbo.
........
r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines
Removed uses of dict.has_key() from distutils, and uses of
callable() from copy_reg.py, so the interpreter now starts up
without warnings when '-3' is given. More work like this needs to
be done in the rest of the stdlib.
........
r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines
configure.ac: Remove the configure check for _Bool, it is already done in the
top-level Python configure script.
configure, fficonfig.h.in: regenerated.
........
r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines
Replace 'has_key()' with 'in'.
Replace 'raise Error, stuff' with 'raise Error(stuff)'.
........
r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line
Update more instances of has_key().
........
r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines
Fix a few typos and layout glitches (more work is needed).
Move 2.5 news to Misc/HISTORY.
........
r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines
#2079: typo in userdict docs.
........
r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines
Part of #2154: minimal syntax fixes in doc example snippets.
........
r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line
First draft for itertools.product(). Docs and other updates forthcoming.
........
r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line
Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery)
........
r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines
A lot more typo fixes by Ori Avtalion.
........
r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines
Don't reference pyshell.
........
r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines
Another fix.
........
2008-02-22 12:37:40 -04:00
|
|
|
* With :mod:`cProfile`, each caller is preceded by three numbers: the number of
|
2007-08-15 11:28:22 -03:00
|
|
|
times this specific call was made, and the total and cumulative times spent in
|
|
|
|
the current function while it was invoked by this specific caller.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. method:: Stats.print_callees(*restrictions)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This method for the :class:`Stats` class prints a list of all function that were
|
|
|
|
called by the indicated function. Aside from this reversal of direction of
|
|
|
|
calls (re: called vs was called by), the arguments and ordering are identical to
|
|
|
|
the :meth:`print_callers` method.
|
|
|
|
|
|
|
|
|
|
|
|
.. _profile-limits:
|
|
|
|
|
|
|
|
Limitations
|
|
|
|
===========
|
|
|
|
|
|
|
|
One limitation has to do with accuracy of timing information. There is a
|
|
|
|
fundamental problem with deterministic profilers involving accuracy. The most
|
|
|
|
obvious restriction is that the underlying "clock" is only ticking at a rate
|
|
|
|
(typically) of about .001 seconds. Hence no measurements will be more accurate
|
|
|
|
than the underlying clock. If enough measurements are taken, then the "error"
|
|
|
|
will tend to average out. Unfortunately, removing this first error induces a
|
|
|
|
second source of error.
|
|
|
|
|
|
|
|
The second problem is that it "takes a while" from when an event is dispatched
|
|
|
|
until the profiler's call to get the time actually *gets* the state of the
|
|
|
|
clock. Similarly, there is a certain lag when exiting the profiler event
|
|
|
|
handler from the time that the clock's value was obtained (and then squirreled
|
|
|
|
away), until the user's code is once again executing. As a result, functions
|
|
|
|
that are called many times, or call many functions, will typically accumulate
|
|
|
|
this error. The error that accumulates in this fashion is typically less than
|
|
|
|
the accuracy of the clock (less than one clock tick), but it *can* accumulate
|
|
|
|
and become very significant.
|
|
|
|
|
|
|
|
The problem is more important with :mod:`profile` than with the lower-overhead
|
|
|
|
:mod:`cProfile`. For this reason, :mod:`profile` provides a means of
|
|
|
|
calibrating itself for a given platform so that this error can be
|
|
|
|
probabilistically (on the average) removed. After the profiler is calibrated, it
|
|
|
|
will be more accurate (in a least square sense), but it will sometimes produce
|
|
|
|
negative numbers (when call counts are exceptionally low, and the gods of
|
|
|
|
probability work against you :-). ) Do *not* be alarmed by negative numbers in
|
|
|
|
the profile. They should *only* appear if you have calibrated your profiler,
|
|
|
|
and the results are actually better than without calibration.
|
|
|
|
|
|
|
|
|
|
|
|
.. _profile-calibration:
|
|
|
|
|
|
|
|
Calibration
|
|
|
|
===========
|
|
|
|
|
|
|
|
The profiler of the :mod:`profile` module subtracts a constant from each event
|
|
|
|
handling time to compensate for the overhead of calling the time function, and
|
|
|
|
socking away the results. By default, the constant is 0. The following
|
|
|
|
procedure can be used to obtain a better constant for a given platform (see
|
|
|
|
discussion in section Limitations above). ::
|
|
|
|
|
|
|
|
import profile
|
|
|
|
pr = profile.Profile()
|
|
|
|
for i in range(5):
|
2007-09-04 04:15:32 -03:00
|
|
|
print(pr.calibrate(10000))
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The method executes the number of Python calls given by the argument, directly
|
|
|
|
and again under the profiler, measuring the time for both. It then computes the
|
|
|
|
hidden overhead per profiler event, and returns that as a float. For example,
|
|
|
|
on an 800 MHz Pentium running Windows 2000, and using Python's time.clock() as
|
|
|
|
the timer, the magical number is about 12.5e-6.
|
|
|
|
|
|
|
|
The object of this exercise is to get a fairly consistent result. If your
|
|
|
|
computer is *very* fast, or your timer function has poor resolution, you might
|
|
|
|
have to pass 100000, or even 1000000, to get consistent results.
|
|
|
|
|
2008-05-12 15:05:20 -03:00
|
|
|
When you have a consistent answer, there are three ways you can use it::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
import profile
|
|
|
|
|
|
|
|
# 1. Apply computed bias to all Profile instances created hereafter.
|
|
|
|
profile.Profile.bias = your_computed_bias
|
|
|
|
|
|
|
|
# 2. Apply computed bias to a specific Profile instance.
|
|
|
|
pr = profile.Profile()
|
|
|
|
pr.bias = your_computed_bias
|
|
|
|
|
|
|
|
# 3. Specify computed bias in instance constructor.
|
|
|
|
pr = profile.Profile(bias=your_computed_bias)
|
|
|
|
|
|
|
|
If you have a choice, you are better off choosing a smaller constant, and then
|
|
|
|
your results will "less often" show up as negative in profile statistics.
|
|
|
|
|
|
|
|
|
|
|
|
.. _profiler-extensions:
|
|
|
|
|
|
|
|
Extensions --- Deriving Better Profilers
|
|
|
|
========================================
|
|
|
|
|
|
|
|
The :class:`Profile` class of both modules, :mod:`profile` and :mod:`cProfile`,
|
|
|
|
were written so that derived classes could be developed to extend the profiler.
|
|
|
|
The details are not described here, as doing this successfully requires an
|
|
|
|
expert understanding of how the :class:`Profile` class works internally. Study
|
|
|
|
the source code of the module carefully if you want to pursue this.
|
|
|
|
|
|
|
|
If all you want to do is change how current time is determined (for example, to
|
|
|
|
force use of wall-clock time or elapsed process time), pass the timing function
|
|
|
|
you want to the :class:`Profile` class constructor::
|
|
|
|
|
|
|
|
pr = profile.Profile(your_time_func)
|
|
|
|
|
|
|
|
The resulting profiler will then call :func:`your_time_func`.
|
|
|
|
|
|
|
|
:class:`profile.Profile`
|
|
|
|
:func:`your_time_func` should return a single number, or a list of numbers whose
|
|
|
|
sum is the current time (like what :func:`os.times` returns). If the function
|
|
|
|
returns a single time number, or the list of returned numbers has length 2, then
|
|
|
|
you will get an especially fast version of the dispatch routine.
|
|
|
|
|
|
|
|
Be warned that you should calibrate the profiler class for the timer function
|
|
|
|
that you choose. For most machines, a timer that returns a lone integer value
|
|
|
|
will provide the best results in terms of low overhead during profiling.
|
|
|
|
(:func:`os.times` is *pretty* bad, as it returns a tuple of floating point
|
|
|
|
values). If you want to substitute a better timer in the cleanest fashion,
|
|
|
|
derive a class and hardwire a replacement dispatch method that best handles your
|
|
|
|
timer call, along with the appropriate calibration constant.
|
|
|
|
|
|
|
|
:class:`cProfile.Profile`
|
2008-05-11 11:30:18 -03:00
|
|
|
:func:`your_time_func` should return a single number. If it returns
|
2007-08-15 11:28:22 -03:00
|
|
|
integers, you can also invoke the class constructor with a second argument
|
|
|
|
specifying the real duration of one unit of time. For example, if
|
|
|
|
:func:`your_integer_time_func` returns times measured in thousands of seconds,
|
2010-09-21 11:48:28 -03:00
|
|
|
you would construct the :class:`Profile` instance as follows::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
pr = profile.Profile(your_integer_time_func, 0.001)
|
|
|
|
|
|
|
|
As the :mod:`cProfile.Profile` class cannot be calibrated, custom timer
|
|
|
|
functions should be used with care and should be as fast as possible. For the
|
|
|
|
best results with a custom timer, it might be necessary to hard-code it in the C
|
|
|
|
source of the internal :mod:`_lsprof` module.
|