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