Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 1 | \section{\module{timeit} --- |
| 2 | Measure execution time of small code snippets} |
| 3 | |
| 4 | \declaremodule{standard}{timeit} |
| 5 | \modulesynopsis{Measure the execution time of small code snippets.} |
| 6 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 7 | \versionadded{2.3} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 8 | \index{Benchmarking} |
| 9 | \index{Performance} |
| 10 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 11 | This module provides a simple way to time small bits of Python code. |
| 12 | It has both command line as well as callable interfaces. It avoids a |
| 13 | number of common traps for measuring execution times. See also Tim |
| 14 | Peters' introduction to the ``Algorithms'' chapter in the |
| 15 | \citetitle{Python Cookbook}, published by O'Reilly. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 16 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 17 | The module defines the following public class: |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 18 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 19 | \begin{classdesc}{Timer}{\optional{stmt=\code{'pass'} |
| 20 | \optional{, setup=\code{'pass'} |
| 21 | \optional{, timer=<timer function>}}}} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 22 | Class for timing execution speed of small code snippets. |
| 23 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 24 | The constructor takes a statement to be timed, an additional statement |
| 25 | used for setup, and a timer function. Both statements default to |
| 26 | \code{'pass'}; the timer function is platform-dependent (see the |
| 27 | module doc string). The statements may contain newlines, as long as |
| 28 | they don't contain multi-line string literals. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 29 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 30 | To measure the execution time of the first statement, use the |
| 31 | \method{timeit()} method. The \method{repeat()} method is a |
| 32 | convenience to call \method{timeit()} multiple times and return a list |
| 33 | of results. |
| 34 | \end{classdesc} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 35 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 36 | \begin{methoddesc}{print_exc}{\optional{file=\constant{None}}} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 37 | Helper to print a traceback from the timed code. |
| 38 | |
| 39 | Typical use: |
| 40 | |
| 41 | \begin{verbatim} |
| 42 | t = Timer(...) # outside the try/except |
| 43 | try: |
| 44 | t.timeit(...) # or t.repeat(...) |
| 45 | except: |
| 46 | t.print_exc() |
| 47 | \end{verbatim} |
| 48 | |
| 49 | The advantage over the standard traceback is that source lines in the |
| 50 | compiled template will be displayed. |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 51 | The optional \var{file} argument directs where the traceback is sent; |
| 52 | it defaults to \code{sys.stderr}. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 53 | \end{methoddesc} |
| 54 | |
Raymond Hettinger | 52136a8 | 2003-05-10 03:35:37 +0000 | [diff] [blame] | 55 | \begin{methoddesc}{repeat}{\optional{repeat\code{=3} \optional{, |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 56 | number\code{=1000000}}}} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 57 | Call \method{timeit()} a few times. |
| 58 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 59 | This is a convenience function that calls the \method{timeit()} |
| 60 | repeatedly, returning a list of results. The first argument specifies |
| 61 | how many times to call \method{timeit()}. The second argument |
| 62 | specifies the \var{number} argument for \function{timeit()}. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 63 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 64 | \begin{notice} |
| 65 | It's tempting to calculate mean and standard deviation from the result |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 66 | vector and report these. However, this is not very useful. In a typical |
| 67 | case, the lowest value gives a lower bound for how fast your machine can run |
| 68 | the given code snippet; higher values in the result vector are typically not |
| 69 | caused by variability in Python's speed, but by other processes interfering |
| 70 | with your timing accuracy. So the \function{min()} of the result is |
| 71 | probably the only number you should be interested in. After that, you |
| 72 | should look at the entire vector and apply common sense rather than |
| 73 | statistics. |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 74 | \end{notice} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 75 | \end{methoddesc} |
| 76 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 77 | \begin{methoddesc}{timeit}{\optional{number\code{=1000000}}} |
| 78 | Time \var{number} executions of the main statement. |
| 79 | This executes the setup statement once, and then |
| 80 | returns the time it takes to execute the main statement a number of |
| 81 | times, measured in seconds as a float. The argument is the number of |
| 82 | times through the loop, defaulting to one million. The main |
| 83 | statement, the setup statement and the timer function to be used are |
| 84 | passed to the constructor. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 85 | \end{methoddesc} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 86 | |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 87 | |
| 88 | \subsection{Command Line Interface} |
| 89 | |
| 90 | When called as a program from the command line, the following form is used: |
| 91 | |
| 92 | \begin{verbatim} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 93 | python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...] |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 94 | \end{verbatim} |
| 95 | |
| 96 | where the following options are understood: |
| 97 | |
| 98 | \begin{description} |
Raymond Hettinger | 9d12ab5 | 2003-08-31 04:20:12 +0000 | [diff] [blame] | 99 | \item[-n N/\longprogramopt{number=N}] how many times to execute 'statement' |
| 100 | \item[-r N/\longprogramopt{repeat=N}] how many times to repeat the timer (default 3) |
| 101 | \item[-s S/\longprogramopt{setup=S}] statement to be executed once initially (default |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 102 | \code{'pass'}) |
Raymond Hettinger | 9d12ab5 | 2003-08-31 04:20:12 +0000 | [diff] [blame] | 103 | \item[-t/\longprogramopt{time}] use \function{time.time()} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 104 | (default on all platforms but Windows) |
Raymond Hettinger | 9d12ab5 | 2003-08-31 04:20:12 +0000 | [diff] [blame] | 105 | \item[-c/\longprogramopt{clock}] use \function{time.clock()} (default on Windows) |
| 106 | \item[-v/\longprogramopt{verbose}] print raw timing results; repeat for more digits |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 107 | precision |
Raymond Hettinger | 9d12ab5 | 2003-08-31 04:20:12 +0000 | [diff] [blame] | 108 | \item[-h/\longprogramopt{help}] print a short usage message and exit |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 109 | \end{description} |
| 110 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 111 | A multi-line statement may be given by specifying each line as a |
| 112 | separate statement argument; indented lines are possible by enclosing |
| 113 | an argument in quotes and using leading spaces. Multiple |
| 114 | \programopt{-s} options are treated similarly. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 115 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 116 | If \programopt{-n} is not given, a suitable number of loops is |
| 117 | calculated by trying successive powers of 10 until the total time is |
| 118 | at least 0.2 seconds. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 119 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 120 | The default timer function is platform dependent. On Windows, |
| 121 | \function{time.clock()} has microsecond granularity but |
| 122 | \function{time.time()}'s granularity is 1/60th of a second; on \UNIX, |
| 123 | \function{time.clock()} has 1/100th of a second granularity and |
| 124 | \function{time.time()} is much more precise. On either platform, the |
Martin v. Löwis | 7bdc484 | 2003-09-20 11:09:28 +0000 | [diff] [blame] | 125 | default timer functions measure wall clock time, not the CPU time. |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 126 | This means that other processes running on the same computer may |
| 127 | interfere with the timing. The best thing to do when accurate timing |
| 128 | is necessary is to repeat the timing a few times and use the best |
| 129 | time. The \programopt{-r} option is good for this; the default of 3 |
| 130 | repetitions is probably enough in most cases. On \UNIX, you can use |
| 131 | \function{time.clock()} to measure CPU time. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 132 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 133 | \begin{notice} |
| 134 | There is a certain baseline overhead associated with executing a |
| 135 | pass statement. The code here doesn't try to hide it, but you |
| 136 | should be aware of it. The baseline overhead can be measured by |
| 137 | invoking the program without arguments. |
| 138 | \end{notice} |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 139 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 140 | The baseline overhead differs between Python versions! Also, to |
| 141 | fairly compare older Python versions to Python 2.3, you may want to |
| 142 | use Python's \programopt{-O} option for the older versions to avoid |
| 143 | timing \code{SET_LINENO} instructions. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 144 | |
| 145 | \subsection{Examples} |
| 146 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 147 | Here are two example sessions (one using the command line, one using |
| 148 | the module interface) that compare the cost of using |
| 149 | \function{hasattr()} vs. \keyword{try}/\keyword{except} to test for |
| 150 | missing and present object attributes. |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 151 | |
| 152 | \begin{verbatim} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 153 | % timeit.py 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass' |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 154 | 100000 loops, best of 3: 15.7 usec per loop |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 155 | % timeit.py 'if hasattr(str, "__nonzero__"): pass' |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 156 | 100000 loops, best of 3: 4.26 usec per loop |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 157 | % timeit.py 'try:' ' int.__nonzero__' 'except AttributeError:' ' pass' |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 158 | 1000000 loops, best of 3: 1.43 usec per loop |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 159 | % timeit.py 'if hasattr(int, "__nonzero__"): pass' |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 160 | 100000 loops, best of 3: 2.23 usec per loop |
| 161 | \end{verbatim} |
| 162 | |
| 163 | \begin{verbatim} |
| 164 | >>> import timeit |
| 165 | >>> s = """\ |
| 166 | ... try: |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 167 | ... str.__nonzero__ |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 168 | ... except AttributeError: |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 169 | ... pass |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 170 | ... """ |
| 171 | >>> t = timeit.Timer(stmt=s) |
| 172 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) |
| 173 | 17.09 usec/pass |
| 174 | >>> s = """\ |
| 175 | ... if hasattr(str, '__nonzero__'): pass |
| 176 | ... """ |
| 177 | >>> t = timeit.Timer(stmt=s) |
| 178 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) |
| 179 | 4.85 usec/pass |
| 180 | >>> s = """\ |
| 181 | ... try: |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 182 | ... int.__nonzero__ |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 183 | ... except AttributeError: |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 184 | ... pass |
Skip Montanaro | ca65274 | 2003-04-09 01:38:53 +0000 | [diff] [blame] | 185 | ... """ |
| 186 | >>> t = timeit.Timer(stmt=s) |
| 187 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) |
| 188 | 1.97 usec/pass |
| 189 | >>> s = """\ |
| 190 | ... if hasattr(int, '__nonzero__'): pass |
| 191 | ... """ |
| 192 | >>> t = timeit.Timer(stmt=s) |
| 193 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) |
| 194 | 3.15 usec/pass |
| 195 | \end{verbatim} |
Skip Montanaro | a30dffb | 2003-05-09 18:21:02 +0000 | [diff] [blame] | 196 | |
| 197 | To give the \module{timeit} module access to functions you |
| 198 | define, you can pass a \code{setup} parameter which contains an import |
| 199 | statement: |
| 200 | |
| 201 | \begin{verbatim} |
| 202 | def test(): |
| 203 | "Stupid test function" |
| 204 | L = [] |
| 205 | for i in range(100): |
| 206 | L.append(i) |
| 207 | |
| 208 | if __name__=='__main__': |
| 209 | from timeit import Timer |
| 210 | t = Timer("test()", "from __main__ import test") |
| 211 | print t.timeit() |
| 212 | \end{verbatim} |