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