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