<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<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">
<LINK REL="next" HREF="profile-limits.html">
<LINK REL="previous" HREF="Deterministic_Profiling.html">
<LINK REL="up" HREF="profile.html">
<LINK REL="next" HREF="profile-stats.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="profile-stats.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="profile.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="Deterministic_Profiling.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="profile-stats.html">10.5.1 The Stats Class</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="profile.html">10. The Python Profiler</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="Deterministic_Profiling.html">10.4 What Is Deterministic</A></span>
<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-1568"><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>
<tt>profile.run('main()')</tt>, and hence the exec'ed string is
<tt>'main()'</tt>.  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: <tt>Ordered by: standard name</tt>, indicates that
the text string in the far right column was used to sort the output.
The column headings include:

<P>
<DL>
<DD><P>
<DT><STRONG>ncalls </STRONG>
<DD>for the number of calls, 

<P>
<DT><STRONG>tottime </STRONG>
<DD>for the total time spent in the given function (and excluding time
made in calls to sub-functions),

<P>
<DT><STRONG>percall </STRONG>
<DD>is the quotient of <tt>tottime</tt> divided by <tt>ncalls</tt>
<P>
<DT><STRONG>cumtime </STRONG>
<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>
<DT><STRONG>percall </STRONG>
<DD>is the quotient of <tt>cumtime</tt> divided by primitive calls

<P>
<DT><STRONG>filename:lineno(function) </STRONG>
<DD>provides the respective data of each function

<P>
</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>
<a name="l2h-1570"></a>

<P>
<dl><dt><b><a name="l2h-1569"><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-->


<UL>
<LI><A NAME="tex2html2756"
 HREF="profile-stats.html">10.5.1 The <tt class=class>Stats</tt> Class </A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="profile-stats.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="profile.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="Deterministic_Profiling.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="profile-stats.html">10.5.1 The Stats Class</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="profile.html">10. The Python Profiler</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="Deterministic_Profiling.html">10.4 What Is Deterministic</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
