📄 119.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> The Python Profiler</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="105.html" class="navtitle">A. The Python Library</a> > <span class="nonavtitle">The Python Profiler</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="118.html" title="The Python Debugger"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=119" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="119.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="120.html" title="Undocumented Modules"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A08%3A16+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105094192221162198121240</font><a href="read4.asp?bookname=0735710910&snode=119&now=5%2F28%2F2002+9%3A08%3A16+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>The Python Profiler</h3>
<p>This section describes the Python profiler梐 tool that can be used to analyze the runtime performance of a program.</p>
<H4><TT Class="monofont">profile</TT></H4>
<P>The <tt clASS="monofont">profile</Tt> module is used to collect profiling information.</p>
<pre>
<b>run(</b><b><i>command</i></b> <b>[,</b> <b><i>filename</i></b><b>])</b> </pRe>
<p>Executes the contents of <I><tt cLass="monofont">command</tT></i>
using the <tt CLASs="monofont">exec</tt> statement under the profiler. <i><TT CLass="monofont">filename</tT></I>
is the name of a file in which raw profiling data is saved. If omitted, a report such as the following is printed to standard output:</P>
<Pre>
126 function calls (6 primitive calls) in 5.130 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.030 0.030 5.070 5.070 <string>:1(?)
121/1 5.020 0.041 5.020 5.020 book.py:11(process)
1 0.020 0.020 5.040 5.040 book.py:5(?)
2 0.000 0.000 0.000 0.000 exceptions.py:101(_ _init_ _)
1 0.060 0.060 5.130 5.130 profile:0(execfile('book.py'))
0 0.000 0.000 profile:0(profiler) </prE>
<P>Different parts of the report generated by <TT class="monofont">run()</tt> are interpreted as follows:</p>
<p><table border="1" CelLspaCing="0" cEllpADDIng="1" wiDTH="100%">
<ColgrOUP Span="2">
<tR>
<TH Valign="top">
<font size="2">
<p><b>Section</b></p>
</fonT></th>
<Th vaLign="top">
<fOnt sIZE="2">
<P><b>Description</b></p>
</fONT></Th>
</tr>
<tR>
<TD ValigN="top">
<FONt size="2">
<p><tt class="monofont">primitive calls</tt></p>
</font></Td>
<tD valIgn="top">
<foNt siZE="2">
<P>Number of nonrecursive function calls</P>
</font></TD>
</TR>
<tr>
<td VALIgn="top">
<foNT SIze="2">
<p><tt class="monofont">ncalls</tt></p>
</font></td>
<tD vaLign="top">
<Font sIze="2">
<p>Total number of calls (including self-recursion)</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font sIZE="2">
<P><tt class="monofont">tottime</tt></p>
</font></td>
<td vaLigN="top">
<fonT size="2">
<P>Time spent in this function (not counting subfunctions)</p>
</foNT></TD>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<P><TT Class="monofont">percall</tt></p>
</font></td>
<td valigN="top">
<foNt siZe="2">
<p><tt ClasS="monofont">tottime/ncalls</TT></P>
</font></TD>
</TR>
<tr>
<td VALIgn="top">
<foNT SIze="2">
<p><tt class="monofont">cumtime</tt></p>
</font></td>
<tD vaLign="top">
<Font sIze="2">
<p>Total time spent in the function</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font sIZE="2">
<P><tt class="monofont">percall</tt></p>
</font></td>
<td vaLigN="top">
<fonT size="2">
<P><tt cLASS="monofont">cumtime/(primitive calls)</tt></p>
</fONT></Td>
</tr>
<tR>
<TD ValigN="top">
<FONt size="2">
<p><tt class="monofont">filename:lineno(function)</tt></p>
</font></Td>
<tD valIgn="top">
<foNt siZE="2">
<P>Location and name of each function</P>
</font></TD>
</TR>
</colgROUP>
</tablE></P>
<P>When there are two numbers in the first column (for example, <Tt class="monofont">"121/1"</tt>), the latter is the number of primitive calls, and the former is the actual number of calls.</p>
<h5>Notes</h5>
<ul>
<li>
<p>Analysis of saved profile data is performed by the <tt clAss="monofont">pstats</Tt> module.</p>
</lI>
<li>
<p>To obtain accurate information, it may be necessary to calibrate the profiler. Please refer to <a TargET="_blank" HRef="http://www.python.org/doc/lib/profile.html">http://www.python.org/doc/lib/profile.html</a> for details.</p>
</LI>
</UL>
<h4><tt cLASS="monofont">pstats</tt></h4>
<p>The <TT CLass="monofont">pstats</tt> module defines a class <tt class="monofont">Stats</tt> that抯 used to analyze the data saved by the <tt clasS="monofont">profile</tt> module.</P>
<pre>
<B>Stats(</b><b><i>filename</i></B><b>)</b> </pRE>
<P>Reads profiling data from <I><tt clASS="monofont">filename</Tt></i>
梐 file previously created by the <tt CLASs="monofont">profile.run()</tt> function. Returns a statistics object that can be used to print reports.</p>
<P>A statistics object <I><TT class="monofont">s</tt></i>
has the following methods:</p>
<pre>
<b><i>s</i></b><b>.strip_dirs()</b> </prE>
<p>Removes leading path information from filenames.</p>
<Pre>
<b><I>s</i></b><b>.add(</b><B><i>filename</i></b> <B>[, ...])</B> </PRe>
<p>Accumulates additional profiling information into the current profile. <i><tT CLAss="monofont">filename</tt></I>
is the name of a file containing data previously saved by <TT Class="monofont">profile.run()</TT>.</P>
<Pre>
<b><i>s</i></b><b>.sort_stats(</b><b><i>key</i></b> <b>[, ...])</b> </pre>
<p>Sorts statistics according to a series of keys. Each key can be one of the following values:</p>
<p><TabLe boRder="1" cEllsPACIng="0" ceLLPAddinG="1" WIDth="100%">
<coLGROup span="2">
<tr>
<th valign="top">
<font SizE="2">
<p><b>Key Name</b></P>
</font></Th>
<th VALIgn="top">
<foNT SIze="2">
<p><b>Description</B></P>
</FOnt></th>
</TR>
<TR>
<td valign="top">
<font size="2">
<p><tt cLasS="monofont">'calls'</tt></p>
</Font></tD>
<td vALIGn="top">
<fonT SIZe="2">
<p>Call count</p>
</fONT></Td>
</tr>
<tR>
<TD Valign="top">
<font size="2">
<p><tt clasS="monofont">'cumulative'</tt></P>
</fonT></td>
<td ValiGN="top">
<FOnt siZE="2">
<P>Cumulative time</P>
</font></TD>
</TR>
<tr>
<td VALIgn="top">
<font size="2">
<p><tt class="monofont">'file'</tt></P>
</foNt></td>
<Td valIgn="top">
<fONT Size="2">
<p>Filename</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font size="2">
<p><tt class="monofont">'module'</tt></p>
</foNt></tD>
<td vAlign="top">
<Font SIZE="2">
<p>Filename</p>
</foNT></TD>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<p><tt class="monofont">'pcalls'</tt></p>
</font></tD>
<td ValiGn="top">
<fonT sizE="2">
<P>Primitive call count</P>
</Font></tD>
</TR>
<Tr>
<td vALIGn="top">
<fonT SIZe="2">
<p><tt class="monofont">'line'</tt></p>
</font></td>
<td ValIgn="top">
<fOnt siZe="2">
<p>Line number</p>
</FONT></td>
</tr>
<TR>
<TD valiGN="top">
<FOnt siZE="2">
<P><Tt class="monofont">'name'</tt></p>
</font></td>
<td valIgn="top">
<Font Size="2">
<p>Function name</P>
</fonT></TD>
</Tr>
<tr>
<tD VALign="top">
<fONT Size="2">
<p><TT CLass="monofont">'nfl'</tt></p>
</font></td>
<td valign="top">
<FonT sizE="2">
<p>Name/file/line</p>
</foNt></td>
</TR>
<TR>
<td vaLIGN="top">
<font SIZE="2">
<p><tt cLASS="monofont">'stdname'</tt></p>
</font></td>
<td valign="top">
<fonT siZe="2">
<p>Standard name</p>
</Font></tD>
</tr>
<tR>
<TD ValigN="top">
<FONt sizE="2">
<P><TT clasS="monofont">'time'</TT></P>
</font></td>
<td valign="top">
<font siZe="2">
<p>Internal time</P>
</fonT></td>
</tr>
</ColgROUP>
</tablE></P>
<P>Time values and call counts are sorted in descending order. Line numbers and filenames are sorted in ascending order.</P>
<pre>
<b><I>s</I></B><B>.print_stats(</b><b><i>restriction</i></B> <B>[, ...])</B> </Pre>
<p>Prints a profile report to standard output. The order is the same as produced by the last <tt class="monofont">sort_stats()</tt> method. The arguments are used to eliminate entries in the report. Each restriction can be an integer to select a maximum line count, a decimal to select a percentage of the lines, or a regular expression to pattern-match against the names that are printed.</p>
<pre>
<b><i>s</i></b><B>.print_callers(</b><b><I>restrictions</i></b> <b>[, ...])</B> </pre>
<p>Prints a list of all functions that called each function in the profile database. The ordering is identical to <Tt clASS="monofont">print_stats()</Tt>. <i><tt CLASs="monofont">restrictions</tt></i>
has the same meaning as for <TT CLass="monofont">print_stats()</tT>.</P>
<PRe>
<b><i>s</i></b><b>.print_callees(</b><b><i>restrictions</i></b> <b>[, ...])</b> </pre>
<p>Prints a list of a functions that were called by each function. <i><tt ClaSs="monofont">restrictions</tt></I>
has the same meaning as for <tt clAss="monofont">print_stats()</tT>.</P>
<H5>Note</H5>
<ul>
<li>
<P>If the <TT Class="monofont">pstats</TT> module is run as a script, it launches an interactive profile statistics browser. Type <TT clasS="monofont">'help'</TT> for a list of the available commands. This is a new Python 2.1 feature.</P>
</li>
</ul></font>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="118.html" title="The Python Debugger"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=119" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="119.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="120.html" title="Undocumented Modules"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -