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