1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>26.5. hotshot — High performance logging profiler — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="26. Debugging and Profiling" href="debug.html" />
34
<link rel="next" title="26.6. timeit — Measure execution time of small code snippets" href="timeit.html" />
35
<link rel="prev" title="26.4. The Python Profilers" href="profile.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="timeit.html" title="26.6. timeit — Measure execution time of small code snippets"
55
accesskey="N">next</a> |</li>
57
<a href="profile.html" title="26.4. The Python Profilers"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="debug.html" accesskey="U">26. Debugging and Profiling</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-hotshot">
78
<span id="hotshot-high-performance-logging-profiler"></span><h1>26.5. <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><code class="xref py py-mod docutils literal"><span class="pre">hotshot</span></code></a> — High performance logging profiler<a class="headerlink" href="#module-hotshot" title="Permalink to this headline">¶</a></h1>
79
<div class="versionadded">
80
<p><span class="versionmodified">New in version 2.2.</span></p>
82
<p>This module provides a nicer interface to the <code class="xref py py-mod docutils literal"><span class="pre">_hotshot</span></code> C module. Hotshot
83
is a replacement for the existing <a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal"><span class="pre">profile</span></code></a> module. As it’s written mostly
84
in C, it should result in a much smaller performance impact than the existing
85
<a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal"><span class="pre">profile</span></code></a> module.</p>
86
<div class="admonition note">
87
<p class="first admonition-title">Note</p>
88
<p class="last">The <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><code class="xref py py-mod docutils literal"><span class="pre">hotshot</span></code></a> module focuses on minimizing the overhead while profiling, at
89
the expense of long data post-processing times. For common usage it is
90
recommended to use <a class="reference internal" href="profile.html#module-cProfile" title="cProfile"><code class="xref py py-mod docutils literal"><span class="pre">cProfile</span></code></a> instead. <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><code class="xref py py-mod docutils literal"><span class="pre">hotshot</span></code></a> is not maintained and
91
might be removed from the standard library in the future.</p>
93
<div class="versionchanged">
94
<p><span class="versionmodified">Changed in version 2.5: </span>The results should be more meaningful than in the past: the timing core
95
contained a critical bug.</p>
97
<div class="admonition note">
98
<p class="first admonition-title">Note</p>
99
<p class="last">The <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><code class="xref py py-mod docutils literal"><span class="pre">hotshot</span></code></a> profiler does not yet work well with threads. It is useful to
100
use an unthreaded script to run the profiler over the code you’re interested in
101
measuring if at all possible.</p>
104
<dt id="hotshot.Profile">
105
<em class="property">class </em><code class="descclassname">hotshot.</code><code class="descname">Profile</code><span class="sig-paren">(</span><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><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile" title="Permalink to this definition">¶</a></dt>
106
<dd><p>The profiler object. The argument <em>logfile</em> is the name of a log file to use for
107
logged profile data. The argument <em>lineevents</em> specifies whether to generate
108
events for every source line, or just on function call/return. It defaults to
109
<code class="docutils literal"><span class="pre">0</span></code> (only log function call/return). The argument <em>linetimings</em> specifies
110
whether to record timing information. It defaults to <code class="docutils literal"><span class="pre">1</span></code> (store timing
114
<div class="section" id="profile-objects">
115
<span id="hotshot-objects"></span><h2>26.5.1. Profile Objects<a class="headerlink" href="#profile-objects" title="Permalink to this headline">¶</a></h2>
116
<p>Profile objects have the following methods:</p>
118
<dt id="hotshot.Profile.addinfo">
119
<code class="descclassname">Profile.</code><code class="descname">addinfo</code><span class="sig-paren">(</span><em>key</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.addinfo" title="Permalink to this definition">¶</a></dt>
120
<dd><p>Add an arbitrary labelled value to the profile output.</p>
124
<dt id="hotshot.Profile.close">
125
<code class="descclassname">Profile.</code><code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.close" title="Permalink to this definition">¶</a></dt>
126
<dd><p>Close the logfile and terminate the profiler.</p>
130
<dt id="hotshot.Profile.fileno">
131
<code class="descclassname">Profile.</code><code class="descname">fileno</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.fileno" title="Permalink to this definition">¶</a></dt>
132
<dd><p>Return the file descriptor of the profiler’s log file.</p>
136
<dt id="hotshot.Profile.run">
137
<code class="descclassname">Profile.</code><code class="descname">run</code><span class="sig-paren">(</span><em>cmd</em><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.run" title="Permalink to this definition">¶</a></dt>
138
<dd><p>Profile an <a class="reference internal" href="../reference/simple_stmts.html#exec"><code class="xref std std-keyword docutils literal"><span class="pre">exec</span></code></a>-compatible string in the script environment. The
139
globals from the <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal"><span class="pre">__main__</span></code></a> module are used as both the globals and locals
144
<dt id="hotshot.Profile.runcall">
145
<code class="descclassname">Profile.</code><code class="descname">runcall</code><span class="sig-paren">(</span><em>func</em>, <em>*args</em>, <em>**keywords</em><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.runcall" title="Permalink to this definition">¶</a></dt>
146
<dd><p>Profile a single call of a callable. Additional positional and keyword arguments
147
may be passed along; the result of the call is returned, and exceptions are
148
allowed to propagate cleanly, while ensuring that profiling is disabled on the
153
<dt id="hotshot.Profile.runctx">
154
<code class="descclassname">Profile.</code><code class="descname">runctx</code><span class="sig-paren">(</span><em>cmd</em>, <em>globals</em>, <em>locals</em><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.runctx" title="Permalink to this definition">¶</a></dt>
155
<dd><p>Evaluate an <a class="reference internal" href="../reference/simple_stmts.html#exec"><code class="xref std std-keyword docutils literal"><span class="pre">exec</span></code></a>-compatible string in a specific environment. The
156
string is compiled before profiling begins.</p>
160
<dt id="hotshot.Profile.start">
161
<code class="descclassname">Profile.</code><code class="descname">start</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.start" title="Permalink to this definition">¶</a></dt>
162
<dd><p>Start the profiler.</p>
166
<dt id="hotshot.Profile.stop">
167
<code class="descclassname">Profile.</code><code class="descname">stop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.Profile.stop" title="Permalink to this definition">¶</a></dt>
168
<dd><p>Stop the profiler.</p>
172
<div class="section" id="module-hotshot.stats">
173
<span id="using-hotshot-data"></span><h2>26.5.2. Using hotshot data<a class="headerlink" href="#module-hotshot.stats" title="Permalink to this headline">¶</a></h2>
174
<div class="versionadded">
175
<p><span class="versionmodified">New in version 2.2.</span></p>
177
<p>This module loads hotshot profiling data into the standard <a class="reference internal" href="profile.html#module-pstats" title="pstats: Statistics object for use with the profiler."><code class="xref py py-mod docutils literal"><span class="pre">pstats</span></code></a> Stats
179
<dl class="function">
180
<dt id="hotshot.stats.load">
181
<code class="descclassname">hotshot.stats.</code><code class="descname">load</code><span class="sig-paren">(</span><em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#hotshot.stats.load" title="Permalink to this definition">¶</a></dt>
182
<dd><p>Load hotshot data from <em>filename</em>. Returns an instance of the
183
<a class="reference internal" href="profile.html#pstats.Stats" title="pstats.Stats"><code class="xref py py-class docutils literal"><span class="pre">pstats.Stats</span></code></a> class.</p>
186
<div class="admonition seealso">
187
<p class="first admonition-title">See also</p>
188
<dl class="last docutils">
189
<dt>Module <a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal"><span class="pre">profile</span></code></a></dt>
190
<dd>The <a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal"><span class="pre">profile</span></code></a> module’s <code class="xref py py-class docutils literal"><span class="pre">Stats</span></code> class</dd>
194
<div class="section" id="example-usage">
195
<span id="hotshot-example"></span><h2>26.5.3. Example Usage<a class="headerlink" href="#example-usage" title="Permalink to this headline">¶</a></h2>
196
<p>Note that this example runs the Python “benchmark” pystones. It can take some
197
time to run, and will produce large output files.</p>
198
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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>
199
<span class="gp">>>> </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="s2">"stones.prof"</span><span class="p">)</span>
200
<span class="gp">>>> </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>
201
<span class="gp">>>> </span><span class="n">prof</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
202
<span class="gp">>>> </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="s2">"stones.prof"</span><span class="p">)</span>
203
<span class="gp">>>> </span><span class="n">stats</span><span class="o">.</span><span class="n">strip_dirs</span><span class="p">()</span>
204
<span class="gp">>>> </span><span class="n">stats</span><span class="o">.</span><span class="n">sort_stats</span><span class="p">(</span><span class="s1">'time'</span><span class="p">,</span> <span class="s1">'calls'</span><span class="p">)</span>
205
<span class="gp">>>> </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>
206
<span class="go"> 850004 function calls in 10.090 CPU seconds</span>
208
<span class="go"> Ordered by: internal time, call count</span>
210
<span class="go"> ncalls tottime percall cumtime percall filename:lineno(function)</span>
211
<span class="go"> 1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)</span>
212
<span class="go"> 150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7)</span>
213
<span class="go"> 50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2)</span>
214
<span class="go"> .</span>
215
<span class="go"> .</span>
216
<span class="go"> .</span>
226
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
227
<div class="sphinxsidebarwrapper">
228
<h3><a href="../contents.html">Table Of Contents</a></h3>
230
<li><a class="reference internal" href="#">26.5. <code class="docutils literal"><span class="pre">hotshot</span></code> — High performance logging profiler</a><ul>
231
<li><a class="reference internal" href="#profile-objects">26.5.1. Profile Objects</a></li>
232
<li><a class="reference internal" href="#module-hotshot.stats">26.5.2. Using hotshot data</a></li>
233
<li><a class="reference internal" href="#example-usage">26.5.3. Example Usage</a></li>
238
<h4>Previous topic</h4>
239
<p class="topless"><a href="profile.html"
240
title="previous chapter">26.4. The Python Profilers</a></p>
242
<p class="topless"><a href="timeit.html"
243
title="next chapter">26.6. <code class="docutils literal"><span class="pre">timeit</span></code> — Measure execution time of small code snippets</a></p>
245
<ul class="this-page-menu">
246
<li><a href="../bugs.html">Report a Bug</a></li>
247
<li><a href="../_sources/library/hotshot.txt"
248
rel="nofollow">Show Source</a></li>
251
<div id="searchbox" style="display: none" role="search">
252
<h3>Quick search</h3>
253
<form class="search" action="../search.html" method="get">
254
<input type="text" name="q" />
255
<input type="submit" value="Go" />
256
<input type="hidden" name="check_keywords" value="yes" />
257
<input type="hidden" name="area" value="default" />
259
<p class="searchtip" style="font-size: 90%">
260
Enter search terms or a module, class or function name.
263
<script type="text/javascript">$('#searchbox').show(0);</script>
266
<div class="clearer"></div>
268
<div class="related" role="navigation" aria-label="related navigation">
271
<li class="right" style="margin-right: 10px">
272
<a href="../genindex.html" title="General Index"
275
<a href="../py-modindex.html" title="Python Module Index"
278
<a href="timeit.html" title="26.6. timeit — Measure execution time of small code snippets"
281
<a href="profile.html" title="26.4. The Python Profilers"
283
<li><img src="../_static/py.png" alt=""
284
style="vertical-align: middle; margin-top: -1px"/></li>
285
<li><a href="https://www.python.org/">Python</a> »</li>
287
<span class="version_switcher_placeholder">2.7.11</span>
288
<a href="../index.html">Documentation</a> »
291
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
292
<li class="nav-item nav-item-2"><a href="debug.html" >26. Debugging and Profiling</a> »</li>
296
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
298
The Python Software Foundation is a non-profit corporation.
299
<a href="https://www.python.org/psf/donations/">Please donate.</a>
301
Last updated on Jan 23, 2016.
302
<a href="../bugs.html">Found a bug</a>?
304
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'