Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Skip Montanaro | cfd5550 | 2003-04-08 19:49:40 +0000 | [diff] [blame] | 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: |
Georg Brandl | 794f5b3 | 2010-08-01 08:52:32 +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 |
Georg Brandl | 794f5b3 | 2010-08-01 08:52:32 +0000 | [diff] [blame] | 22 | --: separate options from statement, use when statement starts with - |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 23 | statement: statement to be timed (default 'pass') |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 24 | |
| 25 | A multi-line statement may be given by specifying each line as a |
| 26 | separate argument; indented lines are possible by enclosing an |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 27 | argument in quotes and using leading spaces. Multiple -s options are |
| 28 | treated similarly. |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 29 | |
| 30 | If -n is not given, a suitable number of loops is calculated by trying |
| 31 | successive powers of 10 until the total time is at least 0.2 seconds. |
| 32 | |
| 33 | The difference in default timer function is because on Windows, |
| 34 | clock() has microsecond granularity but time()'s granularity is 1/60th |
| 35 | of a second; on Unix, clock() has 1/100th of a second granularity and |
| 36 | time() is much more precise. On either platform, the default timer |
Martin v. Löwis | 7bdc484 | 2003-09-20 11:09:28 +0000 | [diff] [blame] | 37 | functions measure wall clock time, not the CPU time. This means that |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 38 | other processes running on the same computer may interfere with the |
| 39 | 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] | 40 | repeat the timing a few times and use the best time. The -r option is |
| 41 | good for this; the default of 3 repetitions is probably enough in most |
| 42 | cases. On Unix, you can use clock() to measure CPU time. |
Guido van Rossum | e8577b7 | 2003-03-06 03:02:10 +0000 | [diff] [blame] | 43 | |
| 44 | Note: there is a certain baseline overhead associated with executing a |
| 45 | 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] | 46 | be aware of it. The baseline overhead can be measured by invoking the |
| 47 | program without arguments. |
| 48 | |
| 49 | The baseline overhead differs between Python versions! Also, to |
| 50 | fairly compare older Python versions to Python 2.3, you may want to |
| 51 | use python -O for the older versions to avoid timing SET_LINENO |
| 52 | instructions. |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 53 | """ |
| 54 | |
Raymond Hettinger | 816ed1b | 2004-01-04 03:47:51 +0000 | [diff] [blame] | 55 | import gc |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 56 | import sys |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 57 | import time |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 58 | try: |
| 59 | import itertools |
| 60 | except ImportError: |
| 61 | # Must be an older Python version (see timeit() below) |
| 62 | itertools = None |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 63 | |
| 64 | __all__ = ["Timer"] |
| 65 | |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 66 | dummy_src_name = "<timeit-src>" |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 67 | default_number = 1000000 |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 68 | default_repeat = 3 |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 69 | |
| 70 | if sys.platform == "win32": |
| 71 | # On Windows, the best timer is time.clock() |
| 72 | default_timer = time.clock |
| 73 | else: |
| 74 | # On most other platforms the best timer is time.time() |
| 75 | default_timer = time.time |
| 76 | |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 77 | # Don't change the indentation of the template; the reindent() calls |
| 78 | # in Timer.__init__() depend on setup being indented 4 spaces and stmt |
| 79 | # being indented 8 spaces. |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 80 | template = """ |
Guido van Rossum | dd42edc | 2003-03-21 14:54:19 +0000 | [diff] [blame] | 81 | def inner(_it, _timer): |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 82 | %(setup)s |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 83 | _t0 = _timer() |
Guido van Rossum | dd42edc | 2003-03-21 14:54:19 +0000 | [diff] [blame] | 84 | for _i in _it: |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 85 | %(stmt)s |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 86 | _t1 = _timer() |
| 87 | return _t1 - _t0 |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 88 | """ |
| 89 | |
| 90 | def reindent(src, indent): |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 91 | """Helper to reindent a multi-line statement.""" |
Guido van Rossum | e05dcce | 2003-03-06 13:09:09 +0000 | [diff] [blame] | 92 | return src.replace("\n", "\n" + " "*indent) |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 94 | def _template_func(setup, func): |
| 95 | """Create a timer function. Used if the "statement" is a callable.""" |
Raymond Hettinger | b646aa1 | 2009-04-03 02:45:36 +0000 | [diff] [blame] | 96 | def inner(_it, _timer, _func=func): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 97 | setup() |
| 98 | _t0 = _timer() |
| 99 | for _i in _it: |
Raymond Hettinger | b646aa1 | 2009-04-03 02:45:36 +0000 | [diff] [blame] | 100 | _func() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 101 | _t1 = _timer() |
| 102 | return _t1 - _t0 |
| 103 | return inner |
| 104 | |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 105 | class Timer: |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 106 | """Class for timing execution speed of small code snippets. |
| 107 | |
| 108 | The constructor takes a statement to be timed, an additional |
| 109 | statement used for setup, and a timer function. Both statements |
| 110 | default to 'pass'; the timer function is platform-dependent (see |
| 111 | module doc string). |
| 112 | |
| 113 | To measure the execution time of the first statement, use the |
| 114 | timeit() method. The repeat() method is a convenience to call |
| 115 | timeit() multiple times and return a list of results. |
| 116 | |
| 117 | The statements may contain newlines, as long as they don't contain |
| 118 | multi-line string literals. |
| 119 | """ |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 120 | |
| 121 | def __init__(self, stmt="pass", setup="pass", timer=default_timer): |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 122 | """Constructor. See class doc string.""" |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 123 | self.timer = timer |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 124 | ns = {} |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 125 | if isinstance(stmt, str): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 126 | stmt = reindent(stmt, 8) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 127 | if isinstance(setup, str): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 128 | setup = reindent(setup, 4) |
| 129 | src = template % {'stmt': stmt, 'setup': setup} |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame^] | 130 | elif callable(setup): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 131 | src = template % {'stmt': stmt, 'setup': '_setup()'} |
| 132 | ns['_setup'] = setup |
| 133 | else: |
| 134 | raise ValueError("setup is neither a string nor callable") |
| 135 | self.src = src # Save for traceback display |
| 136 | code = compile(src, dummy_src_name, "exec") |
| 137 | exec(code, globals(), ns) |
| 138 | self.inner = ns["inner"] |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame^] | 139 | elif callable(stmt): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 140 | self.src = None |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 141 | if isinstance(setup, str): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 142 | _setup = setup |
| 143 | def setup(): |
| 144 | exec(_setup, globals(), ns) |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame^] | 145 | elif not callable(setup): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 146 | raise ValueError("setup is neither a string nor callable") |
| 147 | self.inner = _template_func(setup, stmt) |
| 148 | else: |
| 149 | raise ValueError("stmt is neither a string nor callable") |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 151 | def print_exc(self, file=None): |
| 152 | """Helper to print a traceback from the timed code. |
| 153 | |
| 154 | Typical use: |
| 155 | |
| 156 | t = Timer(...) # outside the try/except |
| 157 | try: |
| 158 | t.timeit(...) # or t.repeat(...) |
| 159 | except: |
| 160 | t.print_exc() |
| 161 | |
| 162 | The advantage over the standard traceback is that source lines |
| 163 | in the compiled template will be displayed. |
| 164 | |
| 165 | The optional file argument directs where the traceback is |
| 166 | sent; it defaults to sys.stderr. |
| 167 | """ |
| 168 | import linecache, traceback |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 169 | if self.src is not None: |
| 170 | linecache.cache[dummy_src_name] = (len(self.src), |
| 171 | None, |
| 172 | self.src.split("\n"), |
| 173 | dummy_src_name) |
| 174 | # else the source is already stored somewhere else |
| 175 | |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 176 | traceback.print_exc(file=file) |
| 177 | |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 178 | def timeit(self, number=default_number): |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 179 | """Time 'number' executions of the main statement. |
| 180 | |
| 181 | To be precise, this executes the setup statement once, and |
| 182 | then returns the time it takes to execute the main statement |
| 183 | a number of times, as a float measured in seconds. The |
| 184 | argument is the number of times through the loop, defaulting |
| 185 | to one million. The main statement, the setup statement and |
| 186 | the timer function to be used are passed to the constructor. |
| 187 | """ |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 188 | if itertools: |
Guido van Rossum | dd42edc | 2003-03-21 14:54:19 +0000 | [diff] [blame] | 189 | it = itertools.repeat(None, number) |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 190 | else: |
Guido van Rossum | dd42edc | 2003-03-21 14:54:19 +0000 | [diff] [blame] | 191 | it = [None] * number |
Raymond Hettinger | 816ed1b | 2004-01-04 03:47:51 +0000 | [diff] [blame] | 192 | gcold = gc.isenabled() |
| 193 | gc.disable() |
Raymond Hettinger | 3a081f5 | 2011-07-29 00:02:04 -0700 | [diff] [blame] | 194 | try: |
| 195 | timing = self.inner(it, self.timer) |
| 196 | finally: |
| 197 | if gcold: |
| 198 | gc.enable() |
Raymond Hettinger | 816ed1b | 2004-01-04 03:47:51 +0000 | [diff] [blame] | 199 | return timing |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 200 | |
| 201 | def repeat(self, repeat=default_repeat, number=default_number): |
Skip Montanaro | fb2a6cc | 2003-04-08 19:40:19 +0000 | [diff] [blame] | 202 | """Call timeit() a few times. |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 203 | |
Skip Montanaro | fb2a6cc | 2003-04-08 19:40:19 +0000 | [diff] [blame] | 204 | This is a convenience function that calls the timeit() |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 205 | repeatedly, returning a list of results. The first argument |
Skip Montanaro | fb2a6cc | 2003-04-08 19:40:19 +0000 | [diff] [blame] | 206 | specifies how many times to call timeit(), defaulting to 3; |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 207 | the second argument specifies the timer argument, defaulting |
| 208 | to one million. |
Guido van Rossum | 5573541 | 2003-03-06 16:11:17 +0000 | [diff] [blame] | 209 | |
| 210 | Note: it's tempting to calculate mean and standard deviation |
| 211 | from the result vector and report these. However, this is not |
| 212 | very useful. In a typical case, the lowest value gives a |
| 213 | lower bound for how fast your machine can run the given code |
| 214 | snippet; higher values in the result vector are typically not |
| 215 | caused by variability in Python's speed, but by other |
| 216 | processes interfering with your timing accuracy. So the min() |
| 217 | of the result is probably the only number you should be |
| 218 | interested in. After that, you should look at the entire |
| 219 | vector and apply common sense rather than statistics. |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 220 | """ |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 221 | r = [] |
| 222 | for i in range(repeat): |
| 223 | t = self.timeit(number) |
| 224 | r.append(t) |
| 225 | return r |
| 226 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 227 | def timeit(stmt="pass", setup="pass", timer=default_timer, |
| 228 | number=default_number): |
| 229 | """Convenience function to create Timer object and call timeit method.""" |
| 230 | return Timer(stmt, setup, timer).timeit(number) |
| 231 | |
| 232 | def repeat(stmt="pass", setup="pass", timer=default_timer, |
| 233 | repeat=default_repeat, number=default_number): |
| 234 | """Convenience function to create Timer object and call repeat method.""" |
| 235 | return Timer(stmt, setup, timer).repeat(repeat, number) |
| 236 | |
R David Murray | a88da67 | 2011-03-16 17:32:27 -0400 | [diff] [blame] | 237 | def main(args=None, *, _wrap_timer=None): |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 238 | """Main program, used when run as a script. |
| 239 | |
R David Murray | a88da67 | 2011-03-16 17:32:27 -0400 | [diff] [blame] | 240 | The optional 'args' argument specifies the command line to be parsed, |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 241 | defaulting to sys.argv[1:]. |
| 242 | |
| 243 | The return value is an exit code to be passed to sys.exit(); it |
| 244 | may be None to indicate success. |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 245 | |
| 246 | When an exception happens during timing, a traceback is printed to |
| 247 | stderr and the return value is 1. Exceptions at other times |
| 248 | (including the template compilation) are not caught. |
R David Murray | a88da67 | 2011-03-16 17:32:27 -0400 | [diff] [blame] | 249 | |
| 250 | '_wrap_timer' is an internal interface used for unit testing. If it |
| 251 | is not None, it must be a callable that accepts a timer function |
| 252 | and returns another timer function (used for unit testing). |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 253 | """ |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 254 | if args is None: |
| 255 | args = sys.argv[1:] |
| 256 | import getopt |
| 257 | try: |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 258 | opts, args = getopt.getopt(args, "n:s:r:tcvh", |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 259 | ["number=", "setup=", "repeat=", |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 260 | "time", "clock", "verbose", "help"]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 261 | except getopt.error as err: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 262 | print(err) |
| 263 | print("use -h/--help for command line help") |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 264 | return 2 |
| 265 | timer = default_timer |
| 266 | stmt = "\n".join(args) or "pass" |
| 267 | number = 0 # auto-determine |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 268 | setup = [] |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 269 | repeat = default_repeat |
| 270 | verbose = 0 |
| 271 | precision = 3 |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 272 | for o, a in opts: |
| 273 | if o in ("-n", "--number"): |
| 274 | number = int(a) |
| 275 | if o in ("-s", "--setup"): |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 276 | setup.append(a) |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 277 | if o in ("-r", "--repeat"): |
| 278 | repeat = int(a) |
| 279 | if repeat <= 0: |
| 280 | repeat = 1 |
Guido van Rossum | e8577b7 | 2003-03-06 03:02:10 +0000 | [diff] [blame] | 281 | if o in ("-t", "--time"): |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 282 | timer = time.time |
Guido van Rossum | e8577b7 | 2003-03-06 03:02:10 +0000 | [diff] [blame] | 283 | if o in ("-c", "--clock"): |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 284 | timer = time.clock |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 285 | if o in ("-v", "--verbose"): |
| 286 | if verbose: |
| 287 | precision += 1 |
| 288 | verbose += 1 |
Guido van Rossum | e8577b7 | 2003-03-06 03:02:10 +0000 | [diff] [blame] | 289 | if o in ("-h", "--help"): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 290 | print(__doc__, end=' ') |
Guido van Rossum | e8577b7 | 2003-03-06 03:02:10 +0000 | [diff] [blame] | 291 | return 0 |
Guido van Rossum | 6e31aad | 2003-03-07 01:33:18 +0000 | [diff] [blame] | 292 | setup = "\n".join(setup) or "pass" |
Raymond Hettinger | 22952a3 | 2003-05-20 04:59:56 +0000 | [diff] [blame] | 293 | # Include the current directory, so that local imports work (sys.path |
| 294 | # contains the directory of this script, rather than the current |
| 295 | # directory) |
| 296 | import os |
| 297 | sys.path.insert(0, os.curdir) |
R David Murray | a88da67 | 2011-03-16 17:32:27 -0400 | [diff] [blame] | 298 | if _wrap_timer is not None: |
| 299 | timer = _wrap_timer(timer) |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 300 | t = Timer(stmt, setup, timer) |
| 301 | if number == 0: |
| 302 | # determine number so that 0.2 <= total time < 2.0 |
| 303 | for i in range(1, 10): |
| 304 | number = 10**i |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 305 | try: |
| 306 | x = t.timeit(number) |
| 307 | except: |
| 308 | t.print_exc() |
| 309 | return 1 |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 310 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 311 | print("%d loops -> %.*g secs" % (number, precision, x)) |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 312 | if x >= 0.2: |
| 313 | break |
Guido van Rossum | 538f1d8 | 2003-03-14 17:21:00 +0000 | [diff] [blame] | 314 | try: |
| 315 | r = t.repeat(repeat, number) |
| 316 | except: |
| 317 | t.print_exc() |
| 318 | return 1 |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 319 | best = min(r) |
Guido van Rossum | 0070f00 | 2003-03-15 12:25:00 +0000 | [diff] [blame] | 320 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 321 | print("raw times:", " ".join(["%.*g" % (precision, x) for x in r])) |
| 322 | print("%d loops," % number, end=' ') |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 323 | usec = best * 1e6 / number |
Guido van Rossum | 5717208 | 2003-10-20 23:38:28 +0000 | [diff] [blame] | 324 | if usec < 1000: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 325 | print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) |
Guido van Rossum | 5717208 | 2003-10-20 23:38:28 +0000 | [diff] [blame] | 326 | else: |
| 327 | msec = usec / 1000 |
| 328 | if msec < 1000: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 329 | print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) |
Guido van Rossum | 5717208 | 2003-10-20 23:38:28 +0000 | [diff] [blame] | 330 | else: |
| 331 | sec = msec / 1000 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 332 | print("best of %d: %.*g sec per loop" % (repeat, precision, sec)) |
Guido van Rossum | b7ab600 | 2003-03-06 02:32:19 +0000 | [diff] [blame] | 333 | return None |
Guido van Rossum | b3f09d4 | 2003-03-05 23:31:58 +0000 | [diff] [blame] | 334 | |
| 335 | if __name__ == "__main__": |
| 336 | sys.exit(main()) |