blob: 54db4067855c87ebc744f6efa61b8d2cdd41ecaf [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001#! /usr/bin/env python
2
3"""Tool for measuring execution time of small code snippets.
4
5This module avoids a number of common traps for measuring execution
6times. See also Tim Peters' introduction to the Algorithms chapter in
7the Python Cookbook, published by O'Reilly.
8
9Library usage: see the Timer class.
10
11Command line usage:
12 python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement]
13
14Options:
15 -n/--number N: how many times to execute 'statement' (default: see below)
16 -r/--repeat N: how many times to repeat the timer (default 3)
17 -s/--setup S: statement to be executed once initially (default 'pass')
18 -t/--time: use time.time() (default on Unix)
19 -c/--clock: use time.clock() (default on Windows)
20 -v/--verbose: print raw timing results; repeat for more digits precision
21 -h/--help: print this usage message and exit
22 statement: statement to be timed (default 'pass')
23
24A multi-line statement may be given by specifying each line as a
25separate argument; indented lines are possible by enclosing an
26argument in quotes and using leading spaces. Multiple -s options are
27treated similarly.
28
29If -n is not given, a suitable number of loops is calculated by trying
30successive powers of 10 until the total time is at least 0.2 seconds.
31
32The difference in default timer function is because on Windows,
33clock() has microsecond granularity but time()'s granularity is 1/60th
34of a second; on Unix, clock() has 1/100th of a second granularity and
35time() is much more precise. On either platform, the default timer
36functions measure wall clock time, not the CPU time. This means that
37other processes running on the same computer may interfere with the
38timing. The best thing to do when accurate timing is necessary is to
39repeat the timing a few times and use the best time. The -r option is
40good for this; the default of 3 repetitions is probably enough in most
41cases. On Unix, you can use clock() to measure CPU time.
42
43Note: there is a certain baseline overhead associated with executing a
44pass statement. The code here doesn't try to hide it, but you should
45be aware of it. The baseline overhead can be measured by invoking the
46program without arguments.
47
48The baseline overhead differs between Python versions! Also, to
49fairly compare older Python versions to Python 2.3, you may want to
50use python -O for the older versions to avoid timing SET_LINENO
51instructions.
52"""
53
54import gc
55import sys
56import time
57try:
58 import itertools
59except ImportError:
60 # Must be an older Python version (see timeit() below)
61 itertools = None
62
63__all__ = ["Timer"]
64
65dummy_src_name = "<timeit-src>"
66default_number = 1000000
67default_repeat = 3
68
69if sys.platform == "win32":
70 # On Windows, the best timer is time.clock()
71 default_timer = time.clock
72else:
73 # On most other platforms the best timer is time.time()
74 default_timer = time.time
75
76# Don't change the indentation of the template; the reindent() calls
77# in Timer.__init__() depend on setup being indented 4 spaces and stmt
78# being indented 8 spaces.
79template = """
80def inner(_it, _timer):
81 %(setup)s
82 _t0 = _timer()
83 for _i in _it:
84 %(stmt)s
85 _t1 = _timer()
86 return _t1 - _t0
87"""
88
89def reindent(src, indent):
90 """Helper to reindent a multi-line statement."""
91 return src.replace("\n", "\n" + " "*indent)
92
93class Timer:
94 """Class for timing execution speed of small code snippets.
95
96 The constructor takes a statement to be timed, an additional
97 statement used for setup, and a timer function. Both statements
98 default to 'pass'; the timer function is platform-dependent (see
99 module doc string).
100
101 To measure the execution time of the first statement, use the
102 timeit() method. The repeat() method is a convenience to call
103 timeit() multiple times and return a list of results.
104
105 The statements may contain newlines, as long as they don't contain
106 multi-line string literals.
107 """
108
109 def __init__(self, stmt="pass", setup="pass", timer=default_timer):
110 """Constructor. See class doc string."""
111 self.timer = timer
112 stmt = reindent(stmt, 8)
113 setup = reindent(setup, 4)
114 src = template % {'stmt': stmt, 'setup': setup}
115 self.src = src # Save for traceback display
116 code = compile(src, dummy_src_name, "exec")
117 ns = {}
118 exec code in globals(), ns
119 self.inner = ns["inner"]
120
121 def print_exc(self, file=None):
122 """Helper to print a traceback from the timed code.
123
124 Typical use:
125
126 t = Timer(...) # outside the try/except
127 try:
128 t.timeit(...) # or t.repeat(...)
129 except:
130 t.print_exc()
131
132 The advantage over the standard traceback is that source lines
133 in the compiled template will be displayed.
134
135 The optional file argument directs where the traceback is
136 sent; it defaults to sys.stderr.
137 """
138 import linecache, traceback
139 linecache.cache[dummy_src_name] = (len(self.src),
140 None,
141 self.src.split("\n"),
142 dummy_src_name)
143 traceback.print_exc(file=file)
144
145 def timeit(self, number=default_number):
146 """Time 'number' executions of the main statement.
147
148 To be precise, this executes the setup statement once, and
149 then returns the time it takes to execute the main statement
150 a number of times, as a float measured in seconds. The
151 argument is the number of times through the loop, defaulting
152 to one million. The main statement, the setup statement and
153 the timer function to be used are passed to the constructor.
154 """
155 if itertools:
156 it = itertools.repeat(None, number)
157 else:
158 it = [None] * number
159 gcold = gc.isenabled()
160 try:
161 gc.disable()
162 except NotImplementedError:
163 pass # ignore on platforms like Jython
164 timing = self.inner(it, self.timer)
165 if gcold:
166 gc.enable()
167 return timing
168
169 def repeat(self, repeat=default_repeat, number=default_number):
170 """Call timeit() a few times.
171
172 This is a convenience function that calls the timeit()
173 repeatedly, returning a list of results. The first argument
174 specifies how many times to call timeit(), defaulting to 3;
175 the second argument specifies the timer argument, defaulting
176 to one million.
177
178 Note: it's tempting to calculate mean and standard deviation
179 from the result vector and report these. However, this is not
180 very useful. In a typical case, the lowest value gives a
181 lower bound for how fast your machine can run the given code
182 snippet; higher values in the result vector are typically not
183 caused by variability in Python's speed, but by other
184 processes interfering with your timing accuracy. So the min()
185 of the result is probably the only number you should be
186 interested in. After that, you should look at the entire
187 vector and apply common sense rather than statistics.
188 """
189 r = []
190 for i in range(repeat):
191 t = self.timeit(number)
192 r.append(t)
193 return r
194
195def main(args=None):
196 """Main program, used when run as a script.
197
198 The optional argument specifies the command line to be parsed,
199 defaulting to sys.argv[1:].
200
201 The return value is an exit code to be passed to sys.exit(); it
202 may be None to indicate success.
203
204 When an exception happens during timing, a traceback is printed to
205 stderr and the return value is 1. Exceptions at other times
206 (including the template compilation) are not caught.
207 """
208 if args is None:
209 args = sys.argv[1:]
210 import getopt
211 try:
212 opts, args = getopt.getopt(args, "n:s:r:tcvh",
213 ["number=", "setup=", "repeat=",
214 "time", "clock", "verbose", "help"])
215 except getopt.error, err:
216 print err
217 print "use -h/--help for command line help"
218 return 2
219 timer = default_timer
220 stmt = "\n".join(args) or "pass"
221 number = 0 # auto-determine
222 setup = []
223 repeat = default_repeat
224 verbose = 0
225 precision = 3
226 for o, a in opts:
227 if o in ("-n", "--number"):
228 number = int(a)
229 if o in ("-s", "--setup"):
230 setup.append(a)
231 if o in ("-r", "--repeat"):
232 repeat = int(a)
233 if repeat <= 0:
234 repeat = 1
235 if o in ("-t", "--time"):
236 timer = time.time
237 if o in ("-c", "--clock"):
238 timer = time.clock
239 if o in ("-v", "--verbose"):
240 if verbose:
241 precision += 1
242 verbose += 1
243 if o in ("-h", "--help"):
244 print __doc__,
245 return 0
246 setup = "\n".join(setup) or "pass"
247 # Include the current directory, so that local imports work (sys.path
248 # contains the directory of this script, rather than the current
249 # directory)
250 import os
251 sys.path.insert(0, os.curdir)
252 t = Timer(stmt, setup, timer)
253 if number == 0:
254 # determine number so that 0.2 <= total time < 2.0
255 for i in range(1, 10):
256 number = 10**i
257 try:
258 x = t.timeit(number)
259 except:
260 t.print_exc()
261 return 1
262 if verbose:
263 print "%d loops -> %.*g secs" % (number, precision, x)
264 if x >= 0.2:
265 break
266 try:
267 r = t.repeat(repeat, number)
268 except:
269 t.print_exc()
270 return 1
271 best = min(r)
272 if verbose:
273 print "raw times:", " ".join(["%.*g" % (precision, x) for x in r])
274 print "%d loops," % number,
275 usec = best * 1e6 / number
276 if usec < 1000:
277 print "best of %d: %.*g usec per loop" % (repeat, precision, usec)
278 else:
279 msec = usec / 1000
280 if msec < 1000:
281 print "best of %d: %.*g msec per loop" % (repeat, precision, msec)
282 else:
283 sec = msec / 1000
284 print "best of %d: %.*g sec per loop" % (repeat, precision, sec)
285 return None
286
287if __name__ == "__main__":
288 sys.exit(main())