236.html

来自「Python Ebook Python&XML」· HTML 代码 · 共 411 行 · 第 1/2 页

HTML
411
字号
								<tT CLAss="monofont">cumtime</tt> is the total time spent in this and all subfunctions (that is, from invocation till exit). This figure is accurate even for recursive functions.</P>

						</P>
<P>
							<P>
								<a name="idx1073750434"></a>
								<tt class="monofont">percall</tt> is the quotient of <tt claSs="monofont">cumtime</tT> divided by primitive calls.</p>

						</p>
<p>
							<P>
								<a namE="idx1073750435"></a>
								<tt CLASs="monofont">filename:lineno(function)</tt> provides the respective data of each function.<a NAME="idx1073750436"></a>
								<a naME="idx1073750437"></A>
							</P>

						</p>
</bloCKQUote>
					<p>When two numbers are in the first column (for instance, 43/3), 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.<a name="idx1073750438"></a>
						<a name="idx1073750439"></a>
					</p>

				
				
					<h4>



Analyzing Profiles with the <tt ClaSs="monofont">pstats</tt> Module</H4>
					<p>The <tt cLass="monofont">pstats</TT> module analyzes the data collected by the Python <TT clasS="monofont">profile</TT> module. The following example demonstrates how we can use this module to manipulate the information generated by the <Tt claSS="monofont">profile</TT> module:</p>

					<pre>
						
&gt;&gt;&gt; import profile, pstats
&gt;&gt;&gt; def main():
...     for n in xrange(3):
...         print n
...
&gt;&gt;&gt; p = profile.Profile()
&gt;&gt;&gt; p.run("main()")
0
1
2
&lt;profile.Profile instance at 7c2c20&gt;
&gt;&gt;&gt; s = pstats.Stats(p)
&gt;&gt;&gt; s.sort_stats("time", "name").print_stats()
3 function calls in 58.727 CPU seconds
   Ordered by: internal time, function name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   58.727   58.727   58.727   58.727 profile:0(main())
        1    0.000    0.000    0.000    0.000 &lt;stdin&gt;:1(main)
        1    0.000    0.000    0.000    0.000 &lt;string&gt;:1(?)
        0    0.000             0.000          profile:0(profiler)
&lt;pstats.Stats instance at 7c2280&gt;
&gt;&gt;&gt;

					</PRE>

					<P>This module exposes the <a name="idx1073750440"></a>
						<a name="idx1073750441"></a>
						<tt class="monofont">Stats(filename, ...)</tT> class. This class is used for creating reports from data generated by the Profile class. It imports data either by direct access to members of Profile class, or by reading in a dictionary that was emitted (viamarshal) from the Profile class. When you want to review the profile, you should use the methods in the <tt ClasS="monofont">pstats</tt> module. Typically you would load the statistics data as follows:</p>

					<pRe>
						
import pstats
p = pstats.Stats('fooprof')

					</prE>

					<P>The class <TT clasS="monofont">Stats</TT> (the previous 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 <Tt claSS="monofont">profile.run(),</TT> the result of three method calls was printed:</p>

					<pre>
						
p.strip_dirs().sort_stats(-1).print_stats()

					</PRE>

					<P>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 (this is to comply with the semantics of the old profiler). The third method printed out all the statistics. You might try the following sort calls:</p>

					<pre>
						
p.sort_stats('name')
p.print_stats()

					</pre>

					<p>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>

					<pre>
						
p.sort_stats('cumulative').print_stats(10)

					</pre>

					<p>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 previous line is what you would use.<a namE="idx1073750442"></a>
						<a Name="idx1073750443"></A>
						<a namE="idx1073750444"></a>
						<a nAME="idx1073750445"></A>
						<a namE="idx1073750446"></A>
						<A Name="idx1073750447"></a>
						<A NAMe="idx1073750448"></a>
						<a nAME="idx1073750449"></A>
					</p>

					<p>If you were looking to see what functions were looping a lot, and taking a lot of time, you would do</p>

					<pre>
						
p.sort_stats('time').print_stats(10)

					</pre>

					<p>This sorts according to time spent within each function, and then prints the statistics for the top ten functions.</p>

					<p>You might also try</p>

					<pre>
						
p.sort_stats('file').print_stats('__init__')

					</pre>

					<p>This will sort all the statistics by filename, and then print out statistics for only the class init methods (because they are spelled with <Tt cLass="monofont">__init__</Tt> in them). The <tt cLass="monofont">sort_stats()</TT> method takes an arbitrary number of quoted strings to select the sort order. For example, <TT clasS="monofont">sort_stats('time', 'name')</TT> sorts on the major key of <Tt claSS="monofont">"internal function time"</TT>, and on the minor key of <tt clASS="monofont">'the name of the function'.</Tt> As one final example, you could try:</p>

					<pre>
						
p.sort_stats('time', 'cum').print_stats(.5, 'init')

					</pre>

					<p>This line sorts stats 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% (.5) of its original size, and then only lines containing <tt class="monofont">"init"</tt> are maintained, and that sub-sub-list is printed.</p>

					<dIv cLass="note"><P clasS="notetitle"><b>Note</b></p><P>

						<P>All the print methods take an argument that indicates how many lines to print. If the arg is a floating point number between 0 and 1.0, it is taken as a decimal percentage of the available lines to be printed (for example, .1 means print 10% of all available lines). If it is an integer, it is taken to mean the number of lines of data that you want to have printed.</P>

					</P></div>
<bR>
<BR>

					<P>If you wondered what functions called the previous functions, you could now (p is still sorted according to the last criteria) do</p>

					<pre>
						
p.print_callers(.5, 'init')

					</PRE>

					<P>You would get a list of callers for each of the listed functions.</p>

					<p>All methods from the Stats class return <tt CLASs="monofont">self,</tt> so you can string together commands such as</p>

					<pre>
						
Stats('foo', 'goo').strip_dirs().sort_stats('calls').}
print_stats(5).print_callers(5)

					</pre>

					<p>This class constructor creates an instance of a statistics object from a filename (or set of filenames). Stats objects are manipulated by methods in order to print useful reports.</p>

					<p>The file selected by the previous constructor must have been created by the corresponding version of profile. 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 (for example, the standard system profiler).</p>

					<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="monofont">Stats</tt> object, the <tT clasS="monofont">add()</tt> method can be used. This can be used to average out the statistics for a short running program to increase the accuracy.</p>

					<P>The following methods are exposed by the <A NAme="idx1073750450"></a>
						<a NAME="idx1073750451"></a>
						<a naME="idx1073750452"></A>
						<Tt claSS="monofont">Stats</TT> class.<a name="idx1073750453"></a>
						<a name="idx1073750454"></a>
						<a name="idx1073750455"></a>
						<a nAme="idx1073750456"></A>
						<a naMe="idx1073750457"></a>
						<a nAme="idx1073750458"></a>
						<A NAMe="idx1073750459"></a>
						<a nAME="idx1073750460"></A>
					</p>

					<bloCKQUote>
<p><A NAMe="idx1073750461"></a><a name="idx1073750462"></a>
							<p><tt class="monofont">strip_dirs()</tt>棤
    
								This method for the <tt ClaSs="monofont">Stats</tt> class removes all leading path information from filenames. 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 striped 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 <Tt claSs="monofont">strip_dirs()</tt> causes two function names to be indistinguishable (that is, they are on the same line of the same filename, and have the same function name), the statistics for these two entries are accumulated into a single entry.</P>

						</P>
<P><A name="idx1073750463"></A>
							<A NAme="idx1073750464"></a>
							<p><TT CLass="monofont">add(filename, ...)</tT>棤
    
								This method of the <TT Class="monofont">Stats</tt> class accumulates additional profiling information into the current profiling object. Its arguments should refer to filenames created by the corresponding version of <tt class="monofont">profile.run().</tt> Statistics for identically named (file, line, name) functions are automatically accumulated into single function statistics.</p>

						</p>
<p><a nAme="idx1073750465"></A><a naMe="idx1073750466"></a>
							<p><tT claSS="monofont">sort_stats(key, ...)</TT>棤
    
								This method modifies the <tt clASS="monofont">Stats</Tt> object by sorting it according to the supplied criteria. The argument is typically a string identifying the basis of a sort (for example: <tt cLASS="monofont">"time"</tt> or <tt CLASs="monofont">"name".</tt>)</p>

						</p>
</blockquote>
					<p>When more than one key is provided, additional keys are used as secondary criteria when equality exists in all keys previously selected. For example, <tt clAss="monofont">sort_stats('name', 'file')</Tt> will sort all the entries according to their function name and resolve all ties (identical function names) by sorting by filename.</p>

					<p>Abbreviations can be used for any key names as long as the abbreviation is unambiguous. The keys currently defined are shown in <A href="236#3.html">Table 17.6</A>.</p>

					<a nAME="3"></A><p><tabLE BOrder="1" CELLspacING="0" Cellpadding="1" width="100%">
<captiOn><h5>Table

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?