Sophie

Sophie

distrib > Mandriva > current > i586 > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 766

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>27.5. hotshot — High performance logging profiler &mdash; Python v2.6.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="27. Debugging and Profiling" href="debug.html" />
    <link rel="next" title="27.6. timeit — Measure execution time of small code snippets" href="timeit.html" />
    <link rel="prev" title="27.4. The Python Profilers" href="profile.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="timeit.html" title="27.6. timeit — Measure execution time of small code snippets"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="profile.html" title="27.4. The Python Profilers"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="debug.html" accesskey="U">27. Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-hotshot">
<h1>27.5. <tt class="xref docutils literal"><span class="pre">hotshot</span></tt> &#8212; High performance logging profiler<a class="headerlink" href="#module-hotshot" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.</span></p>
<p>This module provides a nicer interface to the <tt class="xref docutils literal"><span class="pre">_hotshot</span></tt> C module. Hotshot
is a replacement for the existing <tt class="xref docutils literal"><span class="pre">profile</span></tt> module. As it&#8217;s written mostly
in C, it should result in a much smaller performance impact than the existing
<tt class="xref docutils literal"><span class="pre">profile</span></tt> module.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="xref docutils literal"><span class="pre">hotshot</span></tt> module focuses on minimizing the overhead while profiling, at
the expense of long data post-processing times. For common usage it is
recommended to use <a title="Python profiler" class="reference external" href="profile.html#module-cProfile"><tt class="xref docutils literal"><span class="pre">cProfile</span></tt></a> instead. <tt class="xref docutils literal"><span class="pre">hotshot</span></tt> is not maintained and
might be removed from the standard library in the future.</p>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.5: </span>The results should be more meaningful than in the past: the timing core
contained a critical bug.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="xref docutils literal"><span class="pre">hotshot</span></tt> profiler does not yet work well with threads. It is useful to
use an unthreaded script to run the profiler over the code you&#8217;re interested in
measuring if at all possible.</p>
</div>
<dl class="class">
<dt id="hotshot.Profile">
<em class="property">class </em><tt class="descclassname">hotshot.</tt><tt class="descname">Profile</tt><big>(</big><em>logfile</em><span class="optional">[</span>, <em>lineevents</em><span class="optional">[</span>, <em>linetimings</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#hotshot.Profile" title="Permalink to this definition">¶</a></dt>
<dd>The profiler object. The argument <em>logfile</em> is the name of a log file to use for
logged profile data. The argument <em>lineevents</em> specifies whether to generate
events for every source line, or just on function call/return. It defaults to
<tt class="docutils literal"><span class="pre">0</span></tt> (only log function call/return). The argument <em>linetimings</em> specifies
whether to record timing information. It defaults to <tt class="docutils literal"><span class="pre">1</span></tt> (store timing
information).</dd></dl>

<div class="section" id="profile-objects">
<span id="hotshot-objects"></span><h2>27.5.1. Profile Objects<a class="headerlink" href="#profile-objects" title="Permalink to this headline">¶</a></h2>
<p>Profile objects have the following methods:</p>
<dl class="method">
<dt id="hotshot.Profile.addinfo">
<tt class="descclassname">Profile.</tt><tt class="descname">addinfo</tt><big>(</big><em>key</em>, <em>value</em><big>)</big><a class="headerlink" href="#hotshot.Profile.addinfo" title="Permalink to this definition">¶</a></dt>
<dd>Add an arbitrary labelled value to the profile output.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.close">
<tt class="descclassname">Profile.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.close" title="Permalink to this definition">¶</a></dt>
<dd>Close the logfile and terminate the profiler.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.fileno">
<tt class="descclassname">Profile.</tt><tt class="descname">fileno</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.fileno" title="Permalink to this definition">¶</a></dt>
<dd>Return the file descriptor of the profiler&#8217;s log file.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.run">
<tt class="descclassname">Profile.</tt><tt class="descname">run</tt><big>(</big><em>cmd</em><big>)</big><a class="headerlink" href="#hotshot.Profile.run" title="Permalink to this definition">¶</a></dt>
<dd>Profile an <a class="reference external" href="../reference/simple_stmts.html#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a>-compatible string in the script environment. The
globals from the <a title="The environment where the top-level script is run." class="reference external" href="__main__.html#module-__main__"><tt class="xref docutils literal"><span class="pre">__main__</span></tt></a> module are used as both the globals and locals
for the script.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.runcall">
<tt class="descclassname">Profile.</tt><tt class="descname">runcall</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**keywords</em><big>)</big><a class="headerlink" href="#hotshot.Profile.runcall" title="Permalink to this definition">¶</a></dt>
<dd>Profile a single call of a callable. Additional positional and keyword arguments
may be passed along; the result of the call is returned, and exceptions are
allowed to propagate cleanly, while ensuring that profiling is disabled on the
way out.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.runctx">
<tt class="descclassname">Profile.</tt><tt class="descname">runctx</tt><big>(</big><em>cmd</em>, <em>globals</em>, <em>locals</em><big>)</big><a class="headerlink" href="#hotshot.Profile.runctx" title="Permalink to this definition">¶</a></dt>
<dd>Evaluate an <a class="reference external" href="../reference/simple_stmts.html#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a>-compatible string in a specific environment. The
string is compiled before profiling begins.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.start">
<tt class="descclassname">Profile.</tt><tt class="descname">start</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.start" title="Permalink to this definition">¶</a></dt>
<dd>Start the profiler.</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.stop">
<tt class="descclassname">Profile.</tt><tt class="descname">stop</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.stop" title="Permalink to this definition">¶</a></dt>
<dd>Stop the profiler.</dd></dl>

</div>
<div class="section" id="module-hotshot.stats">
<h2>27.5.2. Using hotshot data<a class="headerlink" href="#module-hotshot.stats" title="Permalink to this headline">¶</a></h2>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.</span></p>
<p>This module loads hotshot profiling data into the standard <a title="Statistics object for use with the profiler." class="reference external" href="profile.html#module-pstats"><tt class="xref docutils literal"><span class="pre">pstats</span></tt></a> Stats
objects.</p>
<dl class="function">
<dt id="hotshot.stats.load">
<tt class="descclassname">hotshot.stats.</tt><tt class="descname">load</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#hotshot.stats.load" title="Permalink to this definition">¶</a></dt>
<dd>Load hotshot data from <em>filename</em>. Returns an instance of the
<a title="pstats.Stats" class="reference external" href="profile.html#pstats.Stats"><tt class="xref docutils literal"><span class="pre">pstats.Stats</span></tt></a> class.</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <tt class="xref docutils literal"><span class="pre">profile</span></tt></dt>
<dd>The <tt class="xref docutils literal"><span class="pre">profile</span></tt> module&#8217;s <tt class="xref docutils literal"><span class="pre">Stats</span></tt> class</dd>
</dl>
</div>
</div>
<div class="section" id="example-usage">
<span id="hotshot-example"></span><h2>27.5.3. Example Usage<a class="headerlink" href="#example-usage" title="Permalink to this headline">¶</a></h2>
<p>Note that this example runs the Python &#8220;benchmark&#8221; pystones.  It can take some
time to run, and will produce large output files.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">hotshot</span><span class="o">,</span> <span class="nn">hotshot.stats</span><span class="o">,</span> <span class="nn">test.pystone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prof</span> <span class="o">=</span> <span class="n">hotshot</span><span class="o">.</span><span class="n">Profile</span><span class="p">(</span><span class="s">&quot;stones.prof&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">benchtime</span><span class="p">,</span> <span class="n">stones</span> <span class="o">=</span> <span class="n">prof</span><span class="o">.</span><span class="n">runcall</span><span class="p">(</span><span class="n">test</span><span class="o">.</span><span class="n">pystone</span><span class="o">.</span><span class="n">pystones</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prof</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span> <span class="o">=</span> <span class="n">hotshot</span><span class="o">.</span><span class="n">stats</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s">&quot;stones.prof&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="o">.</span><span class="n">strip_dirs</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="o">.</span><span class="n">sort_stats</span><span class="p">(</span><span class="s">&#39;time&#39;</span><span class="p">,</span> <span class="s">&#39;calls&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="o">.</span><span class="n">print_stats</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">         850004 function calls in 10.090 CPU seconds</span>

<span class="go">   Ordered by: internal time, call count</span>

<span class="go">   ncalls  tottime  percall  cumtime  percall filename:lineno(function)</span>
<span class="go">        1    3.295    3.295   10.090   10.090 pystone.py:79(Proc0)</span>
<span class="go">   150000    1.315    0.000    1.315    0.000 pystone.py:203(Proc7)</span>
<span class="go">    50000    1.313    0.000    1.463    0.000 pystone.py:229(Func2)</span>
<span class="go"> .</span>
<span class="go"> .</span>
<span class="go"> .</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">27.5. <tt class="docutils literal"><span class="pre">hotshot</span></tt> &#8212; High performance logging profiler</a><ul>
<li><a class="reference external" href="#profile-objects">27.5.1. Profile Objects</a></li>
<li><a class="reference external" href="#module-hotshot.stats">27.5.2. Using hotshot data</a></li>
<li><a class="reference external" href="#example-usage">27.5.3. Example Usage</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="profile.html"
                                  title="previous chapter">27.4. The Python Profilers</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="timeit.html"
                                  title="next chapter">27.6. <tt class="docutils literal"><span class="pre">timeit</span></tt> &#8212; Measure execution time of small code snippets</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/hotshot.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="timeit.html" title="27.6. timeit — Measure execution time of small code snippets"
             >next</a> |</li>
        <li class="right" >
          <a href="profile.html" title="27.4. The Python Profilers"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="debug.html" >27. Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>