| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="prev" href="module-hotshot.html" /> |
| 13 | <link rel="parent" href="profile.html" /> |
| 14 | <link rel="next" href="node464.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>10.10 timeit -- Measure execution time of small code snippets</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="10.9.3 Example Usage" |
| 24 | href="hotshot-example.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="10. The Python Profiler" |
| 27 | href="profile.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="10.10.1 Command Line Interface" |
| 30 | href="node464.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Library Reference</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 37 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="hotshot-example.html">10.9.3 Example Usage</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="profile.html">10. The Python Profiler</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="node464.html">10.10.1 Command Line Interface</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H1><A NAME="SECTION00121000000000000000000"> |
| 55 | 10.10 <tt class="module">timeit</tt> -- |
| 56 | Measure execution time of small code snippets</A> |
| 57 | </H1> |
| 58 | |
| 59 | <P> |
| 60 | <A NAME="module-timeit"></A> |
| 61 | |
| 62 | <P> |
| 63 | |
| 64 | <span class="versionnote">New in version 2.3.</span> |
| 65 | |
| 66 | <a id='l2h-3138' xml:id='l2h-3138'></a> |
| 67 | |
| 68 | <P> |
| 69 | This module provides a simple way to time small bits of Python code. |
| 70 | It has both command line as well as callable interfaces. It avoids a |
| 71 | number of common traps for measuring execution times. See also Tim |
| 72 | Peters' introduction to the ``Algorithms'' chapter in the |
| 73 | <em class="citetitle" |
| 74 | >Python Cookbook</em>, published by O'Reilly. |
| 75 | |
| 76 | <P> |
| 77 | The module defines the following public class: |
| 78 | |
| 79 | <P> |
| 80 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 81 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3134' xml:id='l2h-3134' class="class">Timer</tt></b>(</nobr></td> |
| 82 | <td><var></var><big>[</big><var>stmt=<code>'pass'</code> |
| 83 | </var><big>[</big><var>, setup=<code>'pass'</code> |
| 84 | </var><big>[</big><var>, timer=<timer function></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 85 | <dd> |
| 86 | Class for timing execution speed of small code snippets. |
| 87 | |
| 88 | <P> |
| 89 | The constructor takes a statement to be timed, an additional statement |
| 90 | used for setup, and a timer function. Both statements default to |
| 91 | <code>'pass'</code>; the timer function is platform-dependent (see the |
| 92 | module doc string). The statements may contain newlines, as long as |
| 93 | they don't contain multi-line string literals. |
| 94 | |
| 95 | <P> |
| 96 | To measure the execution time of the first statement, use the |
| 97 | <tt class="method">timeit()</tt> method. The <tt class="method">repeat()</tt> method is a |
| 98 | convenience to call <tt class="method">timeit()</tt> multiple times and return a list |
| 99 | of results. |
| 100 | </dl> |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 104 | <td><nobr><b><tt id='l2h-3135' xml:id='l2h-3135' class="method">print_exc</tt></b>(</nobr></td> |
| 105 | <td><var></var><big>[</big><var>file=<tt class="constant">None</tt></var><big>]</big><var></var>)</td></tr></table></dt> |
| 106 | <dd> |
| 107 | Helper to print a traceback from the timed code. |
| 108 | |
| 109 | <P> |
| 110 | Typical use: |
| 111 | |
| 112 | <P> |
| 113 | <div class="verbatim"><pre> |
| 114 | t = Timer(...) # outside the try/except |
| 115 | try: |
| 116 | t.timeit(...) # or t.repeat(...) |
| 117 | except: |
| 118 | t.print_exc() |
| 119 | </pre></div> |
| 120 | |
| 121 | <P> |
| 122 | The advantage over the standard traceback is that source lines in the |
| 123 | compiled template will be displayed. |
| 124 | The optional <var>file</var> argument directs where the traceback is sent; |
| 125 | it defaults to <code>sys.stderr</code>. |
| 126 | </dl> |
| 127 | |
| 128 | <P> |
| 129 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 130 | <td><nobr><b><tt id='l2h-3136' xml:id='l2h-3136' class="method">repeat</tt></b>(</nobr></td> |
| 131 | <td><var></var><big>[</big><var>repeat<code>=3</code> </var><big>[</big><var>, |
| 132 | number<code>=1000000</code></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 133 | <dd> |
| 134 | Call <tt class="method">timeit()</tt> a few times. |
| 135 | |
| 136 | <P> |
| 137 | This is a convenience function that calls the <tt class="method">timeit()</tt> |
| 138 | repeatedly, returning a list of results. The first argument specifies |
| 139 | how many times to call <tt class="method">timeit()</tt>. The second argument |
| 140 | specifies the <var>number</var> argument for <tt class="function">timeit()</tt>. |
| 141 | |
| 142 | <P> |
| 143 | <div class="note"><b class="label">Note:</b> |
| 144 | It's tempting to calculate mean and standard deviation from the result |
| 145 | vector and report these. However, this is not very useful. In a typical |
| 146 | case, the lowest value gives a lower bound for how fast your machine can run |
| 147 | the given code snippet; higher values in the result vector are typically not |
| 148 | caused by variability in Python's speed, but by other processes interfering |
| 149 | with your timing accuracy. So the <tt class="function">min()</tt> of the result is |
| 150 | probably the only number you should be interested in. After that, you |
| 151 | should look at the entire vector and apply common sense rather than |
| 152 | statistics. |
| 153 | </div> |
| 154 | </dl> |
| 155 | |
| 156 | <P> |
| 157 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 158 | <td><nobr><b><tt id='l2h-3137' xml:id='l2h-3137' class="method">timeit</tt></b>(</nobr></td> |
| 159 | <td><var></var><big>[</big><var>number<code>=1000000</code></var><big>]</big><var></var>)</td></tr></table></dt> |
| 160 | <dd> |
| 161 | Time <var>number</var> executions of the main statement. |
| 162 | This executes the setup statement once, and then |
| 163 | returns the time it takes to execute the main statement a number of |
| 164 | times, measured in seconds as a float. The argument is the number of |
| 165 | times through the loop, defaulting to one million. The main |
| 166 | statement, the setup statement and the timer function to be used are |
| 167 | passed to the constructor. |
| 168 | |
| 169 | <P> |
| 170 | <div class="note"><b class="label">Note:</b> |
| 171 | By default, <tt class="method">timeit()</tt> temporarily turns off garbage collection |
| 172 | during the timing. The advantage of this approach is that it makes |
| 173 | independent timings more comparable. This disadvantage is that GC |
| 174 | may be an important component of the performance of the function being |
| 175 | measured. If so, GC can be re-enabled as the first statement in the |
| 176 | <var>setup</var> string. For example: |
| 177 | <div class="verbatim"><pre> |
| 178 | timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit() |
| 179 | </pre></div> |
| 180 | </div> |
| 181 | </dl> |
| 182 | |
| 183 | <P> |
| 184 | |
| 185 | <p><br /></p><hr class='online-navigation' /> |
| 186 | <div class='online-navigation'> |
| 187 | <!--Table of Child-Links--> |
| 188 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 189 | |
| 190 | <UL CLASS="ChildLinks"> |
| 191 | <LI><A href="node464.html">10.10.1 Command Line Interface</a> |
| 192 | <LI><A href="node465.html">10.10.2 Examples</a> |
| 193 | </ul> |
| 194 | <!--End of Table of Child-Links--> |
| 195 | </div> |
| 196 | |
| 197 | <DIV CLASS="navigation"> |
| 198 | <div class='online-navigation'> |
| 199 | <p></p><hr /> |
| 200 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 201 | <tr> |
| 202 | <td class='online-navigation'><a rel="prev" title="10.9.3 Example Usage" |
| 203 | href="hotshot-example.html"><img src='../icons/previous.png' |
| 204 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 205 | <td class='online-navigation'><a rel="parent" title="10. The Python Profiler" |
| 206 | href="profile.html"><img src='../icons/up.png' |
| 207 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 208 | <td class='online-navigation'><a rel="next" title="10.10.1 Command Line Interface" |
| 209 | href="node464.html"><img src='../icons/next.png' |
| 210 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 211 | <td align="center" width="100%">Python Library Reference</td> |
| 212 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 213 | href="contents.html"><img src='../icons/contents.png' |
| 214 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 215 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 216 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 217 | <td class='online-navigation'><a rel="index" title="Index" |
| 218 | href="genindex.html"><img src='../icons/index.png' |
| 219 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 220 | </tr></table> |
| 221 | <div class='online-navigation'> |
| 222 | <b class="navlabel">Previous:</b> |
| 223 | <a class="sectref" rel="prev" href="hotshot-example.html">10.9.3 Example Usage</A> |
| 224 | <b class="navlabel">Up:</b> |
| 225 | <a class="sectref" rel="parent" href="profile.html">10. The Python Profiler</A> |
| 226 | <b class="navlabel">Next:</b> |
| 227 | <a class="sectref" rel="next" href="node464.html">10.10.1 Command Line Interface</A> |
| 228 | </div> |
| 229 | </div> |
| 230 | <hr /> |
| 231 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 232 | </DIV> |
| 233 | <!--End of Navigation Panel--> |
| 234 | <ADDRESS> |
| 235 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 236 | </ADDRESS> |
| 237 | </BODY> |
| 238 | </HTML> |