📄 module-profile.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>10.5 Reference Manual</title>
<META NAME="description" CONTENT="10.5 Reference Manual">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="profile-limits.html" tppabs="http://www.python.org/doc/current/lib/profile-limits.html">
<LINK REL="previous" href="Deterministic_Profiling.html" tppabs="http://www.python.org/doc/current/lib/Deterministic_Profiling.html">
<LINK REL="up" href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html">
<LINK REL="next" href="profile-stats.html" tppabs="http://www.python.org/doc/current/lib/profile-stats.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="Deterministic_Profiling.html" tppabs="http://www.python.org/doc/current/lib/Deterministic_Profiling.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="profile-stats.html" tppabs="http://www.python.org/doc/current/lib/profile-stats.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="Deterministic_Profiling.html" tppabs="http://www.python.org/doc/current/lib/Deterministic_Profiling.html">10.4 What Is Deterministic</A>
<b class="navlabel">Up:</b> <a class="sectref" href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html">10. The Python Profiler</A>
<b class="navlabel">Next:</b> <a class="sectref" href="profile-stats.html" tppabs="http://www.python.org/doc/current/lib/profile-stats.html">10.5.1 The Stats Class</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0012500000000000000000">
10.5 Reference Manual</A>
</H1>
<P>
<P>
The primary entry point for the profiler is the global function
<tt class="function">profile.run()</tt>. It is typically used to create any profile
information. The reports are formatted and printed using methods of
the class <tt class="class">pstats.Stats</tt>. 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.
<P>
<dl><dt><b><a name='l2h-1953'><tt class='function'>run</tt></a></b> (<var>string</var><big>[</big><var>, filename</var><big>[</big><var>, ...</var><big>]</big><big>]</big>)
<dd>
<P>
This function takes a single argument that has can be passed to the
<tt class="keyword">exec</tt> statement, and an optional file name. In all cases this
routine attempts to <tt class="keyword">exec</tt> 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:
<P>
<dl><dd><pre class="verbatim">
main()
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)
...
</pre></dl>
<P>
The first line indicates that this profile was generated by the call:
<BR><code>profile.run('main()')</code>, and hence the exec'ed string is
<code>'main()'</code>. The second line indicates that 2706 calls were
monitored. Of those calls, 2004 were <i class="dfn">primitive</i>. We define
<i class="dfn">primitive</i> to mean that the call was not induced via recursion.
The next line: <code>Ordered by: standard name</code>, indicates that
the text string in the far right column was used to sort the output.
The column headings include:
<P>
<DL>
<DT><STRONG>ncalls </STRONG></DT>
<DD>for the number of calls,
<P>
</DD>
<DT><STRONG>tottime </STRONG></DT>
<DD>for the total time spent in the given function (and excluding time
made in calls to sub-functions),
<P>
</DD>
<DT><STRONG>percall </STRONG></DT>
<DD>is the quotient of <code>tottime</code> divided by <code>ncalls</code>
<P>
</DD>
<DT><STRONG>cumtime </STRONG></DT>
<DD>is the total time spent in this and all subfunctions (i.e., from
invocation till exit). This figure is accurate <i>even</i> for recursive
functions.
<P>
</DD>
<DT><STRONG>percall </STRONG></DT>
<DD>is the quotient of <code>cumtime</code> divided by primitive calls
<P>
</DD>
<DT><STRONG>filename:lineno(function) </STRONG></DT>
<DD>provides the respective data of each function
<P>
</DD>
</DL>
<P>
When there are two numbers in the first column (e.g.: "<tt class="samp">43/3</tt>"),
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.
<P>
</dl>
<P>
Analysis of the profiler data is done using this class from the
<tt class="module">pstats</tt> module:
<P>
<P>
<dl><dt><b><a name='l2h-1954'><tt class='class'>Stats</tt></a></b> (<var>filename</var><big>[</big><var>, ...</var><big>]</big>)
<dd>
This class constructor creates an instance of a ``statistics object''
from a <var>filename</var> (or set of filenames). <tt class="class">Stats</tt> objects are
manipulated by methods, in order to print useful reports.
<P>
The file selected by the above constructor must have been created by
the corresponding version of <tt class="module">profile</tt>. To be specific, there is
<i>no</i> file compatibility guaranteed with future versions of this
profiler, and there is no compatibility with files produced by other
profilers (e.g., the old system profiler).
<P>
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 <tt class="class">Stats</tt> object, the
<tt class="method">add()</tt> method can be used.
</dl>
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html3665"
href="profile-stats.html" tppabs="http://www.python.org/doc/current/lib/profile-stats.html">10.5.1 The <tt class="class">Stats</tt> Class </A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="Deterministic_Profiling.html" tppabs="http://www.python.org/doc/current/lib/Deterministic_Profiling.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="profile-stats.html" tppabs="http://www.python.org/doc/current/lib/profile-stats.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="Deterministic_Profiling.html" tppabs="http://www.python.org/doc/current/lib/Deterministic_Profiling.html">10.4 What Is Deterministic</A>
<b class="navlabel">Up:</b> <a class="sectref" href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html">10. The Python Profiler</A>
<b class="navlabel">Next:</b> <a class="sectref" href="profile-stats.html" tppabs="http://www.python.org/doc/current/lib/profile-stats.html">10.5.1 The Stats Class</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -