blob: 01129940403dcae7a9416faafd5f41e2433fddeb [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`timeit` --- Measure execution time of small code snippets
2===============================================================
3
4.. module:: timeit
5 :synopsis: Measure the execution time of small code snippets.
6
7
Georg Brandl116aa622007-08-15 14:28:22 +00008.. index::
9 single: Benchmarking
10 single: Performance
11
Raymond Hettingera1993682011-01-27 01:20:32 +000012**Source code:** :source:`Lib/timeit.py`
13
14--------------
15
Georg Brandl116aa622007-08-15 14:28:22 +000016This module provides a simple way to time small bits of Python code. It has both
17command line as well as callable interfaces. It avoids a number of common traps
18for measuring execution times. See also Tim Peters' introduction to the
19"Algorithms" chapter in the Python Cookbook, published by O'Reilly.
20
21The module defines the following public class:
22
23
Georg Brandl7f01a132009-09-16 15:58:14 +000024.. class:: Timer(stmt='pass', setup='pass', timer=<timer function>)
Georg Brandl116aa622007-08-15 14:28:22 +000025
26 Class for timing execution speed of small code snippets.
27
28 The constructor takes a statement to be timed, an additional statement used for
29 setup, and a timer function. Both statements default to ``'pass'``; the timer
Benjamin Peterson0289b152009-06-28 17:22:03 +000030 function is platform-dependent (see the module doc string). *stmt* and *setup*
31 may also contain multiple statements separated by ``;`` or newlines, as long as
32 they don't contain multi-line string literals.
Georg Brandl116aa622007-08-15 14:28:22 +000033
34 To measure the execution time of the first statement, use the :meth:`timeit`
35 method. The :meth:`repeat` method is a convenience to call :meth:`timeit`
36 multiple times and return a list of results.
37
Georg Brandl55ac8f02007-09-01 13:51:09 +000038 The *stmt* and *setup* parameters can also take objects that are callable
39 without arguments. This will embed calls to them in a timer function that
40 will then be executed by :meth:`timeit`. Note that the timing overhead is a
41 little larger in this case because of the extra function calls.
Georg Brandl116aa622007-08-15 14:28:22 +000042
43
Georg Brandl7f01a132009-09-16 15:58:14 +000044.. method:: Timer.print_exc(file=None)
Georg Brandl116aa622007-08-15 14:28:22 +000045
46 Helper to print a traceback from the timed code.
47
48 Typical use::
49
50 t = Timer(...) # outside the try/except
51 try:
52 t.timeit(...) # or t.repeat(...)
53 except:
54 t.print_exc()
55
56 The advantage over the standard traceback is that source lines in the compiled
57 template will be displayed. The optional *file* argument directs where the
58 traceback is sent; it defaults to ``sys.stderr``.
59
60
Georg Brandl7f01a132009-09-16 15:58:14 +000061.. method:: Timer.repeat(repeat=3, number=1000000)
Georg Brandl116aa622007-08-15 14:28:22 +000062
63 Call :meth:`timeit` a few times.
64
65 This is a convenience function that calls the :meth:`timeit` repeatedly,
66 returning a list of results. The first argument specifies how many times to
67 call :meth:`timeit`. The second argument specifies the *number* argument for
68 :func:`timeit`.
69
70 .. note::
71
72 It's tempting to calculate mean and standard deviation from the result vector
73 and report these. However, this is not very useful. In a typical case, the
74 lowest value gives a lower bound for how fast your machine can run the given
75 code snippet; higher values in the result vector are typically not caused by
76 variability in Python's speed, but by other processes interfering with your
77 timing accuracy. So the :func:`min` of the result is probably the only number
78 you should be interested in. After that, you should look at the entire vector
79 and apply common sense rather than statistics.
80
81
Georg Brandl7f01a132009-09-16 15:58:14 +000082.. method:: Timer.timeit(number=1000000)
Georg Brandl116aa622007-08-15 14:28:22 +000083
84 Time *number* executions of the main statement. This executes the setup
85 statement once, and then returns the time it takes to execute the main statement
86 a number of times, measured in seconds as a float. The argument is the number
87 of times through the loop, defaulting to one million. The main statement, the
88 setup statement and the timer function to be used are passed to the constructor.
89
90 .. note::
91
Christian Heimesd8654cf2007-12-02 15:22:16 +000092 By default, :meth:`timeit` temporarily turns off :term:`garbage collection`
93 during the timing. The advantage of this approach is that it makes
94 independent timings more comparable. This disadvantage is that GC may be
95 an important component of the performance of the function being measured.
96 If so, GC can be re-enabled as the first statement in the *setup* string.
97 For example::
Georg Brandl116aa622007-08-15 14:28:22 +000098
99 timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Georg Brandle6bcc912008-05-12 18:05:20 +0000102The module also defines two convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Georg Brandl7f01a132009-09-16 15:58:14 +0000104.. function:: repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000)
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106 Create a :class:`Timer` instance with the given statement, setup code and timer
107 function and run its :meth:`repeat` method with the given repeat count and
108 *number* executions.
109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Georg Brandl7f01a132009-09-16 15:58:14 +0000111.. function:: timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)
Georg Brandl116aa622007-08-15 14:28:22 +0000112
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
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117Command Line Interface
118----------------------
119
120When called as a program from the command line, the following form is used::
121
122 python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
123
Éric Araujo713d3032010-11-18 16:38:46 +0000124Where the following options are understood:
Georg Brandl116aa622007-08-15 14:28:22 +0000125
Éric Araujo713d3032010-11-18 16:38:46 +0000126.. program:: timeit
127
128.. cmdoption:: -n N, --number=N
129
Georg Brandl116aa622007-08-15 14:28:22 +0000130 how many times to execute 'statement'
131
Éric Araujo713d3032010-11-18 16:38:46 +0000132.. cmdoption:: -r N, --repeat=N
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134 how many times to repeat the timer (default 3)
135
Éric Araujo713d3032010-11-18 16:38:46 +0000136.. cmdoption:: -s S, --setup=S
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Éric Araujo713d3032010-11-18 16:38:46 +0000138 statement to be executed once initially (default ``pass``)
139
140.. cmdoption:: -t, --time
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142 use :func:`time.time` (default on all platforms but Windows)
143
Éric Araujo713d3032010-11-18 16:38:46 +0000144.. cmdoption:: -c, --clock
145
Georg Brandl116aa622007-08-15 14:28:22 +0000146 use :func:`time.clock` (default on Windows)
147
Éric Araujo713d3032010-11-18 16:38:46 +0000148.. cmdoption:: -v, --verbose
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150 print raw timing results; repeat for more digits precision
151
Éric Araujo713d3032010-11-18 16:38:46 +0000152.. cmdoption:: -h, --help
153
Georg Brandl116aa622007-08-15 14:28:22 +0000154 print a short usage message and exit
155
156A multi-line statement may be given by specifying each line as a separate
157statement argument; indented lines are possible by enclosing an argument in
158quotes and using leading spaces. Multiple :option:`-s` options are treated
159similarly.
160
161If :option:`-n` is not given, a suitable number of loops is calculated by trying
162successive powers of 10 until the total time is at least 0.2 seconds.
163
164The default timer function is platform dependent. On Windows,
165:func:`time.clock` has microsecond granularity but :func:`time.time`'s
166granularity is 1/60th of a second; on Unix, :func:`time.clock` has 1/100th of a
167second granularity and :func:`time.time` is much more precise. On either
168platform, the default timer functions measure wall clock time, not the CPU time.
169This means that other processes running on the same computer may interfere with
170the timing. The best thing to do when accurate timing is necessary is to repeat
171the timing a few times and use the best time. The :option:`-r` option is good
172for this; the default of 3 repetitions is probably enough in most cases. On
173Unix, you can use :func:`time.clock` to measure CPU time.
174
175.. note::
176
177 There is a certain baseline overhead associated with executing a pass statement.
178 The code here doesn't try to hide it, but you should be aware of it. The
179 baseline overhead can be measured by invoking the program without arguments.
180
181The baseline overhead differs between Python versions! Also, to fairly compare
182older Python versions to Python 2.3, you may want to use Python's :option:`-O`
183option for the older versions to avoid timing ``SET_LINENO`` instructions.
184
185
186Examples
187--------
188
189Here are two example sessions (one using the command line, one using the module
190interface) that compare the cost of using :func:`hasattr` vs.
191:keyword:`try`/:keyword:`except` to test for missing and present object
192attributes. ::
193
Senthil Kumaran2e015352011-08-06 13:37:04 +0800194 $ python -m timeit 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
Georg Brandl116aa622007-08-15 14:28:22 +0000195 100000 loops, best of 3: 15.7 usec per loop
Senthil Kumaran2e015352011-08-06 13:37:04 +0800196 $ python -m timeit 'if hasattr(str, "__bool__"): pass'
Georg Brandl116aa622007-08-15 14:28:22 +0000197 100000 loops, best of 3: 4.26 usec per loop
Senthil Kumaran2e015352011-08-06 13:37:04 +0800198 $ python -m timeit 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
Georg Brandl116aa622007-08-15 14:28:22 +0000199 1000000 loops, best of 3: 1.43 usec per loop
Senthil Kumaran2e015352011-08-06 13:37:04 +0800200 $ python -m timeit 'if hasattr(int, "__bool__"): pass'
Georg Brandl116aa622007-08-15 14:28:22 +0000201 100000 loops, best of 3: 2.23 usec per loop
202
203::
204
205 >>> import timeit
206 >>> s = """\
207 ... try:
208 ... str.__bool__
209 ... except AttributeError:
210 ... pass
211 ... """
212 >>> t = timeit.Timer(stmt=s)
Collin Winterc79461b2007-09-01 23:34:30 +0000213 >>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
Georg Brandl116aa622007-08-15 14:28:22 +0000214 17.09 usec/pass
215 >>> s = """\
216 ... if hasattr(str, '__bool__'): pass
217 ... """
218 >>> t = timeit.Timer(stmt=s)
Collin Winterc79461b2007-09-01 23:34:30 +0000219 >>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
Georg Brandl116aa622007-08-15 14:28:22 +0000220 4.85 usec/pass
221 >>> s = """\
222 ... try:
223 ... int.__bool__
224 ... except AttributeError:
225 ... pass
226 ... """
227 >>> t = timeit.Timer(stmt=s)
Collin Winterc79461b2007-09-01 23:34:30 +0000228 >>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
Georg Brandl116aa622007-08-15 14:28:22 +0000229 1.97 usec/pass
230 >>> s = """\
231 ... if hasattr(int, '__bool__'): pass
232 ... """
233 >>> t = timeit.Timer(stmt=s)
Collin Winterc79461b2007-09-01 23:34:30 +0000234 >>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
Georg Brandl116aa622007-08-15 14:28:22 +0000235 3.15 usec/pass
236
237To give the :mod:`timeit` module access to functions you define, you can pass a
238``setup`` parameter which contains an import statement::
239
240 def test():
Senthil Kumaran2e015352011-08-06 13:37:04 +0800241 """Stupid test function"""
Collin Winterc79461b2007-09-01 23:34:30 +0000242 L = [i for i in range(100)]
Georg Brandl116aa622007-08-15 14:28:22 +0000243
Senthil Kumaran2e015352011-08-06 13:37:04 +0800244 if __name__ == '__main__':
Georg Brandl116aa622007-08-15 14:28:22 +0000245 from timeit import Timer
246 t = Timer("test()", "from __main__ import test")
Collin Winterc79461b2007-09-01 23:34:30 +0000247 print(t.timeit())
Georg Brandl116aa622007-08-15 14:28:22 +0000248