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