[code.view]

[top] / python / PyMOTW / docs / timeit / index.html


<!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>timeit – Time the execution of small bits of Python code. &mdash; Python Module of the Week</title>
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.132',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="top" title="Python Module of the Week" href="../index.html" />
    <link rel="up" title="Debugging and Profiling" href="../profilers.html" />
    <link rel="next" title="trace – Follow Python statements as they are executed" href="../trace/index.html" />
    <link rel="prev" title="profile, cProfile, and pstats – Performance analysis of Python programs." href="../profile/index.html" /> 
  </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="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../trace/index.html" title="trace – Follow Python statements as they are executed"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../profile/index.html" title="profile, cProfile, and pstats – Performance analysis of Python programs."
             accesskey="P">previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="../profilers.html" accesskey="U">Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">timeit &#8211; Time the execution of small bits of Python code.</a><ul>
<li><a class="reference internal" href="#module-contents">Module Contents</a></li>
<li><a class="reference internal" href="#basic-example">Basic Example</a></li>
<li><a class="reference internal" href="#storing-values-in-a-dictionary">Storing Values in a Dictionary</a></li>
<li><a class="reference internal" href="#from-the-command-line">From the Command Line</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../profile/index.html"
                        title="previous chapter">profile, cProfile, and pstats &#8211; Performance analysis of Python programs.</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../trace/index.html"
                        title="next chapter">trace &#8211; Follow Python statements as they are executed</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/timeit/index.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="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-timeit">
<span id="timeit-time-the-execution-of-small-bits-of-python-code"></span><h1>timeit &#8211; Time the execution of small bits of Python code.<a class="headerlink" href="#module-timeit" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Purpose:</th><td class="field-body">Time the execution of small bits of Python code.</td>
</tr>
<tr class="field"><th class="field-name">Python Version:</th><td class="field-body">2.3</td>
</tr>
</tbody>
</table>
<p>The <a class="reference internal" href="#module-timeit" title="timeit: Time the execution of small bits of Python code."><tt class="xref py py-mod docutils literal"><span class="pre">timeit</span></tt></a> module provides a simple interface for determining
the execution time of small bits of Python code. It uses a
platform-specific time function to provide the most accurate time
calculation possible. It reduces the impact of startup or shutdown
costs on the time calculation by executing the code repeatedly.</p>
<div class="section" id="module-contents">
<h2>Module Contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#module-timeit" title="timeit: Time the execution of small bits of Python code."><tt class="xref py py-mod docutils literal"><span class="pre">timeit</span></tt></a> defines a single public class, <tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt>. The
constructor for <tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt> takes a statement to be timed, and a
setup statement (to initialize variables, for example). The Python
statements should be strings and can include embedded newlines.</p>
<p>The <a class="reference internal" href="#module-timeit" title="timeit: Time the execution of small bits of Python code."><tt class="xref py py-func docutils literal"><span class="pre">timeit()</span></tt></a> method runs the setup statement one time, then
executes the primary statement repeatedly and returns the amount of
time which passes. The argument to timeit() controls how many times to
run the statement; the default is 1,000,000.</p>
</div>
<div class="section" id="basic-example">
<h2>Basic Example<a class="headerlink" href="#basic-example" title="Permalink to this headline">¶</a></h2>
<p>To illustrate how the various arguments to <tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt> are used,
here is a simple example which prints an identifying value when each
statement is executed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">timeit</span>

<span class="c"># using setitem</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s">&quot;print &#39;main statement&#39;&quot;</span><span class="p">,</span> <span class="s">&quot;print &#39;setup&#39;&quot;</span><span class="p">)</span>

<span class="k">print</span> <span class="s">&#39;TIMEIT:&#39;</span>
<span class="k">print</span> <span class="n">t</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>

<span class="k">print</span> <span class="s">&#39;REPEAT:&#39;</span>
<span class="k">print</span> <span class="n">t</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>When run, the output is:</p>
<div class="highlight-python"><pre>$ python timeit_example.py

TIMEIT:
setup
main statement
main statement
2.86102294922e-06
REPEAT:
setup
main statement
main statement
setup
main statement
main statement
setup
main statement
main statement
[1.9073486328125e-06, 9.5367431640625e-07, 9.5367431640625e-07]</pre>
</div>
<p>When called, <a class="reference internal" href="#module-timeit" title="timeit: Time the execution of small bits of Python code."><tt class="xref py py-func docutils literal"><span class="pre">timeit()</span></tt></a> runs the setup statement one time, then
calls the main statement count times. It returns a single floating
point value representing the amount of time it took to run the main
statement count times.</p>
<p>When <tt class="xref py py-func docutils literal"><span class="pre">repeat()</span></tt> is used, it calls <a class="reference internal" href="#module-timeit" title="timeit: Time the execution of small bits of Python code."><tt class="xref py py-func docutils literal"><span class="pre">timeit()</span></tt></a> severeal
times (3 in this case) and all of the responses are returned in a
list.</p>
</div>
<div class="section" id="storing-values-in-a-dictionary">
<h2>Storing Values in a Dictionary<a class="headerlink" href="#storing-values-in-a-dictionary" title="Permalink to this headline">¶</a></h2>
<p>For a more complex example, let&#8217;s compare the amount of time it takes
to populate a dictionary with a large number of values using a variety
of methods. First, a few constants are needed to configure the
<tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt>. We&#8217;ll be using a list of tuples containing strings and
integers. The <tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt> will be storing the integers in a
dictionary using the strings as keys.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;header&#39;)}}}</span>
<span class="kn">import</span> <span class="nn">timeit</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="c"># A few constants</span>
<span class="n">range_size</span><span class="o">=</span><span class="mi">1000</span>
<span class="n">count</span><span class="o">=</span><span class="mi">1000</span>
<span class="n">setup_statement</span><span class="o">=</span><span class="s">&quot;l = [ (str(x), x) for x in range(</span><span class="si">%d</span><span class="s">) ]; d = {}&quot;</span> <span class="o">%</span> <span class="n">range_size</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>Next, we can define a short utility function to print the results in a
useful format. The <a class="reference internal" href="#module-timeit" title="timeit: Time the execution of small bits of Python code."><tt class="xref py py-func docutils literal"><span class="pre">timeit()</span></tt></a> method returns the amount of time
it takes to execute the statement repeatedly. The output of
<tt class="xref py py-func docutils literal"><span class="pre">show_results()</span></tt> converts that into the amount of time it takes
per iteration, and then further reduces the value to the amount of
time it takes to store one item in the dictionary (as averages, of
course).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;show_results&#39;)}}}</span>
<span class="k">def</span> <span class="nf">show_results</span><span class="p">(</span><span class="n">result</span><span class="p">):</span>
    <span class="s">&quot;Print results in terms of microseconds per pass and per item.&quot;</span>
    <span class="k">global</span> <span class="n">count</span><span class="p">,</span> <span class="n">range_size</span>
    <span class="n">per_pass</span> <span class="o">=</span> <span class="mi">1000000</span> <span class="o">*</span> <span class="p">(</span><span class="n">result</span> <span class="o">/</span> <span class="n">count</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="si">%.2f</span><span class="s"> usec/pass&#39;</span> <span class="o">%</span> <span class="n">per_pass</span><span class="p">,</span>
    <span class="n">per_item</span> <span class="o">=</span> <span class="n">per_pass</span> <span class="o">/</span> <span class="n">range_size</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="si">%.2f</span><span class="s"> usec/item&#39;</span> <span class="o">%</span> <span class="n">per_item</span>

<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> items&quot;</span> <span class="o">%</span> <span class="n">range_size</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s"> iterations&quot;</span> <span class="o">%</span> <span class="n">count</span>
<span class="k">print</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>To establish a baseline, the first configuration tested will use
<tt class="xref py py-func docutils literal"><span class="pre">__setitem__()</span></tt>.  All of the other variations avoid overwriting
values already in the dictionary, so this simple version should be the
fastest.</p>
<p>Notice that the first argument to <tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt> is a multi-line
string, with indention preserved to ensure that it parses correctly
when run. The second argument is a constant established above to
initialize the list of values and the dictionary.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;setitem&#39;)}}}</span>
<span class="c"># Using __setitem__ without checking for existing values first</span>
<span class="k">print</span> <span class="s">&#39;__setitem__:</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="c"># using setitem</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">for s, i in l:</span>
<span class="s">    d[s] = i</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">,</span>
<span class="n">setup_statement</span><span class="p">)</span>
<span class="n">show_results</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="n">count</span><span class="p">))</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>The next variation uses <tt class="xref py py-func docutils literal"><span class="pre">setdefault()</span></tt> to ensure that values
already in the dictionary are not overwritten.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;setdefault&#39;)}}}</span>
<span class="c"># Using setdefault</span>
<span class="k">print</span> <span class="s">&#39;setdefault:</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">for s, i in l:</span>
<span class="s">    d.setdefault(s, i)</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">,</span>
<span class="n">setup_statement</span><span class="p">)</span>
<span class="n">show_results</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="n">count</span><span class="p">))</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>Another way to avoid overwriting existing values is to use
<tt class="xref py py-func docutils literal"><span class="pre">has_key()</span></tt> to check the contents of the dictionary explicitly.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;has_key&#39;)}}}</span>
<span class="c"># Using has_key</span>
<span class="k">print</span> <span class="s">&#39;has_key:</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="c"># using setitem</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">for s, i in l:</span>
<span class="s">    if not d.has_key(s):</span>
<span class="s">        d[s] = i</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">,</span>
<span class="n">setup_statement</span><span class="p">)</span>
<span class="n">show_results</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="n">count</span><span class="p">))</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>Or by adding the value only if we receive a <a class="reference internal" href="../exceptions/index.html#exceptions-keyerror"><em>KeyError</em></a> exception when looking for the existing value.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;exception&#39;)}}}</span>
<span class="c"># Using exceptions</span>
<span class="k">print</span> <span class="s">&#39;KeyError:</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="c"># using setitem</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">for s, i in l:</span>
<span class="s">    try:</span>
<span class="s">        existing = d[s]</span>
<span class="s">    except KeyError:</span>
<span class="s">        d[s] = i</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">,</span>
<span class="n">setup_statement</span><span class="p">)</span>
<span class="n">show_results</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="n">count</span><span class="p">))</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>And the last method we will test is the (relatively) new form using
&#8220;<tt class="docutils literal"><span class="pre">in</span></tt>&#8221; to determine if a dictionary has a particular key.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># {{{cog include(&#39;timeit/timeit_dictionary.py&#39;, &#39;in&#39;)}}}</span>
<span class="c"># Using &quot;in&quot;</span>
<span class="k">print</span> <span class="s">&#39;&quot;not in&quot;:</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="c"># using setitem</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">for s, i in l:</span>
<span class="s">    if s not in d:</span>
<span class="s">        d[s] = i</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">,</span>
<span class="n">setup_statement</span><span class="p">)</span>
<span class="n">show_results</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="n">count</span><span class="p">))</span>
<span class="c"># {{{end}}}</span>
</pre></div>
</div>
<p>When run, the script produces output similar to this:</p>
<div class="highlight-python"><pre>$ python timeit_dictionary.py

1000 items
1000 iterations

__setitem__:    135.30 usec/pass 0.14 usec/item
setdefault:     275.84 usec/pass 0.28 usec/item
has_key:        203.45 usec/pass 0.20 usec/item
KeyError:       153.10 usec/pass 0.15 usec/item
"not in":       106.02 usec/pass 0.11 usec/item</pre>
</div>
<p>Those times are for a MacBook Pro running Python 2.6. Your times will
be different. Experiment with the <em>range_size</em> and <em>count</em> variables,
since different combinations will produce different results.</p>
</div>
<div class="section" id="from-the-command-line">
<h2>From the Command Line<a class="headerlink" href="#from-the-command-line" title="Permalink to this headline">¶</a></h2>
<p>In addition to the programmatic interface, timeit provides a command
line interface for testing modules without instrumentation.</p>
<p>To run the module, use the new <em class="xref std std-option">-m</em> option to find the module and
treat it as the main program:</p>
<div class="highlight-python"><pre>$ python -m timeit</pre>
</div>
<p>For example, to get help:</p>
<div class="highlight-python"><pre>$ python -m timeit -h

Tool for measuring execution time of small code snippets.

This module avoids a number of common traps for measuring execution
times.  See also Tim Peters' introduction to the Algorithms chapter in
the Python Cookbook, published by O'Reilly.

Library usage: see the Timer class.

Command line usage:
    python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement]

Options:
  -n/--number N: how many times to execute 'statement' (default: see below)
  -r/--repeat N: how many times to repeat the timer (default 3)
  -s/--setup S: statement to be executed once initially (default 'pass')
  -t/--time: use time.time() (default on Unix)
  -c/--clock: use time.clock() (default on Windows)
  -v/--verbose: print raw timing results; repeat for more digits precision
  -h/--help: print this usage message and exit
  statement: statement to be timed (default 'pass')

A multi-line statement may be given by specifying each line as a
separate argument; indented lines are possible by enclosing an
argument in quotes and using leading spaces.  Multiple -s options are
treated similarly.

If -n is not given, a suitable number of loops is calculated by trying
successive powers of 10 until the total time is at least 0.2 seconds.

The difference in default timer function is because on Windows,
clock() has microsecond granularity but time()'s granularity is 1/60th
of a second; on Unix, clock() has 1/100th of a second granularity and
time() is much more precise.  On either platform, the default timer
functions measure wall clock time, not the CPU time.  This means that
other processes running on the same computer may interfere with the
timing.  The best thing to do when accurate timing is necessary is to
repeat the timing a few times and use the best time.  The -r option is
good for this; the default of 3 repetitions is probably enough in most
cases.  On Unix, you can use clock() to measure CPU time.

Note: there is a certain baseline overhead associated with executing a
pass statement.  The code here doesn't try to hide it, but you should
be aware of it.  The baseline overhead can be measured by invoking the
program without arguments.

The baseline overhead differs between Python versions!  Also, to
fairly compare older Python versions to Python 2.3, you may want to
use python -O for the older versions to avoid timing SET_LINENO
instructions.</pre>
</div>
<p>The statement argument works a little differently than the argument to
<tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt>.  Instead of one long string, you pass each line of the
instructions as a separate command line argument. To indent lines
(such as inside a loop), embed spaces in the string by enclosing the
whole thing in quotes. For example:</p>
<div class="highlight-python"><pre>$ python -m timeit -s "d={}" "for i in range(1000):" "  d[str(i)] = i"

1000 loops, best of 3: 390 usec per loop</pre>
</div>
<p>It is also possible to define a function with more complex code, then
import the module and call the function from the command line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test_setitem</span><span class="p">(</span><span class="n">range_size</span><span class="o">=</span><span class="mi">1000</span><span class="p">):</span>
    <span class="n">l</span> <span class="o">=</span> <span class="p">[</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">range_size</span><span class="p">)</span> <span class="p">]</span>
    <span class="n">d</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">s</span><span class="p">,</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
        <span class="n">d</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span>
</pre></div>
</div>
<p>Then to run the test:</p>
<div class="highlight-python"><pre>$ python -m timeit "import timeit_setitem; timeit_setitem.test_setitem()"

1000 loops, best of 3: 557 usec per loop</pre>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://docs.python.org/lib/module-timeit.html">timeit</a></dt>
<dd>Standard library documentation for this module.</dd>
<dt><a class="reference internal" href="../profile/index.html#module-profile" title="profile: Performance analysis of Python programs."><tt class="xref py py-mod docutils literal"><span class="pre">profile</span></tt></a></dt>
<dd>The profile module is also useful for performance analysis.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </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="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../trace/index.html" title="trace – Follow Python statements as they are executed"
             >next</a> |</li>
        <li class="right" >
          <a href="../profile/index.html" title="profile, cProfile, and pstats – Performance analysis of Python programs."
             >previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="../profilers.html" >Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright Doug Hellmann.
      Last updated on Oct 24, 2010.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.

    <br/><a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/88x31.png"/></a>
    
    </div>
  </body>
</html>

[top] / python / PyMOTW / docs / timeit / index.html

contact | logmethods.com