timeit.py 12.1 KB
Newer Older
1
#! /usr/bin/env python3
2

3
"""Tool for measuring execution time of small code snippets.
4

5 6 7
This module avoids a number of common traps for measuring execution
times.  See also Tim Peters' introduction to the Algorithms chapter in
the Python Cookbook, published by O'Reilly.
8

9
Library usage: see the Timer class.
10 11

Command line usage:
12
    python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement]
13 14

Options:
15
  -n/--number N: how many times to execute 'statement' (default: see below)
16
  -r/--repeat N: how many times to repeat the timer (default 3)
17 18
  -s/--setup S: statement to be executed once initially (default 'pass').
                Execution time of this setup statement is NOT timed.
19 20 21
  -p/--process: use time.process_time() (default is time.perf_counter())
  -t/--time: use time.time() (deprecated)
  -c/--clock: use time.clock() (deprecated)
22
  -v/--verbose: print raw timing results; repeat for more digits precision
23
  -u/--unit: set the output time unit (usec, msec, or sec)
24
  -h/--help: print this usage message and exit
25
  --: separate options from statement, use when statement starts with -
26
  statement: statement to be timed (default 'pass')
27 28 29

A multi-line statement may be given by specifying each line as a
separate argument; indented lines are possible by enclosing an
30 31
argument in quotes and using leading spaces.  Multiple -s options are
treated similarly.
32 33 34 35

If -n is not given, a suitable number of loops is calculated by trying
successive powers of 10 until the total time is at least 0.2 seconds.

36
Note: there is a certain baseline overhead associated with executing a
37 38 39 40 41 42 43 44 45 46 47 48 49 50
pass statement.  It differs between versions.  The code here doesn't try
to hide it, but you should be aware of it.  The baseline overhead can be
measured by invoking the program without arguments.

Classes:

    Timer

Functions:

    timeit(string, string) -> float
    repeat(string, string) -> list
    default_timer() -> float

51
"""
52

53
import gc
54 55
import sys
import time
56
import itertools
57

58
__all__ = ["Timer", "timeit", "repeat", "default_timer"]
59

60
dummy_src_name = "<timeit-src>"
61
default_number = 1000000
62
default_repeat = 3
63
default_timer = time.perf_counter
64

65 66
_globals = globals

67 68 69
# Don't change the indentation of the template; the reindent() calls
# in Timer.__init__() depend on setup being indented 4 spaces and stmt
# being indented 8 spaces.
70
template = """
71
def inner(_it, _timer{init}):
72
    {setup}
73
    _t0 = _timer()
74
    for _i in _it:
75
        {stmt}
76 77
    _t1 = _timer()
    return _t1 - _t0
78 79 80
"""

def reindent(src, indent):
81
    """Helper to reindent a multi-line statement."""
82
    return src.replace("\n", "\n" + " "*indent)
83 84

class Timer:
85 86 87 88 89
    """Class for timing execution speed of small code snippets.

    The constructor takes a statement to be timed, an additional
    statement used for setup, and a timer function.  Both statements
    default to 'pass'; the timer function is platform-dependent (see
90 91 92
    module doc string).  If 'globals' is specified, the code will be
    executed within that namespace (as opposed to inside timeit's
    namespace).
93 94 95 96 97 98 99 100

    To measure the execution time of the first statement, use the
    timeit() method.  The repeat() method is a convenience to call
    timeit() multiple times and return a list of results.

    The statements may contain newlines, as long as they don't contain
    multi-line string literals.
    """
101

102 103
    def __init__(self, stmt="pass", setup="pass", timer=default_timer,
                 globals=None):
104
        """Constructor.  See class doc string."""
105
        self.timer = timer
106 107
        local_ns = {}
        global_ns = _globals() if globals is None else globals
108 109 110 111
        init = ''
        if isinstance(setup, str):
            # Check that the code can be compiled outside a function
            compile(setup, dummy_src_name, "exec")
112
            stmtprefix = setup + '\n'
113 114 115 116
            setup = reindent(setup, 4)
        elif callable(setup):
            local_ns['_setup'] = setup
            init += ', _setup=_setup'
117
            stmtprefix = ''
118 119 120
            setup = '_setup()'
        else:
            raise ValueError("setup is neither a string nor callable")
121
        if isinstance(stmt, str):
122
            # Check that the code can be compiled outside a function
123
            compile(stmtprefix + stmt, dummy_src_name, "exec")
124
            stmt = reindent(stmt, 8)
125
        elif callable(stmt):
126 127 128
            local_ns['_stmt'] = stmt
            init += ', _stmt=_stmt'
            stmt = '_stmt()'
129 130
        else:
            raise ValueError("stmt is neither a string nor callable")
131 132 133 134 135
        src = template.format(stmt=stmt, setup=setup, init=init)
        self.src = src  # Save for traceback display
        code = compile(src, dummy_src_name, "exec")
        exec(code, global_ns, local_ns)
        self.inner = local_ns["inner"]
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    def print_exc(self, file=None):
        """Helper to print a traceback from the timed code.

        Typical use:

            t = Timer(...)       # outside the try/except
            try:
                t.timeit(...)    # or t.repeat(...)
            except:
                t.print_exc()

        The advantage over the standard traceback is that source lines
        in the compiled template will be displayed.

        The optional file argument directs where the traceback is
        sent; it defaults to sys.stderr.
        """
        import linecache, traceback
155 156 157 158 159 160 161
        if self.src is not None:
            linecache.cache[dummy_src_name] = (len(self.src),
                                               None,
                                               self.src.split("\n"),
                                               dummy_src_name)
        # else the source is already stored somewhere else

162 163
        traceback.print_exc(file=file)

164
    def timeit(self, number=default_number):
165 166 167 168 169 170 171 172 173
        """Time 'number' executions of the main statement.

        To be precise, this executes the setup statement once, and
        then returns the time it takes to execute the main statement
        a number of times, as a float measured in seconds.  The
        argument is the number of times through the loop, defaulting
        to one million.  The main statement, the setup statement and
        the timer function to be used are passed to the constructor.
        """
174
        it = itertools.repeat(None, number)
175 176
        gcold = gc.isenabled()
        gc.disable()
177 178 179 180 181
        try:
            timing = self.inner(it, self.timer)
        finally:
            if gcold:
                gc.enable()
182
        return timing
183 184

    def repeat(self, repeat=default_repeat, number=default_number):
185
        """Call timeit() a few times.
186

187
        This is a convenience function that calls the timeit()
188
        repeatedly, returning a list of results.  The first argument
189
        specifies how many times to call timeit(), defaulting to 3;
190 191
        the second argument specifies the timer argument, defaulting
        to one million.
192 193 194 195 196 197 198 199 200 201 202

        Note: it's tempting to calculate mean and standard deviation
        from the result vector and report these.  However, this is not
        very useful.  In a typical case, the lowest value gives a
        lower bound for how fast your machine can run the given code
        snippet; higher values in the result vector are typically not
        caused by variability in Python's speed, but by other
        processes interfering with your timing accuracy.  So the min()
        of the result is probably the only number you should be
        interested in.  After that, you should look at the entire
        vector and apply common sense rather than statistics.
203
        """
204 205 206 207 208 209
        r = []
        for i in range(repeat):
            t = self.timeit(number)
            r.append(t)
        return r

210
def timeit(stmt="pass", setup="pass", timer=default_timer,
211
           number=default_number, globals=None):
212
    """Convenience function to create Timer object and call timeit method."""
213
    return Timer(stmt, setup, timer, globals).timeit(number)
214 215

def repeat(stmt="pass", setup="pass", timer=default_timer,
216
           repeat=default_repeat, number=default_number, globals=None):
217
    """Convenience function to create Timer object and call repeat method."""
218
    return Timer(stmt, setup, timer, globals).repeat(repeat, number)
219

220
def main(args=None, *, _wrap_timer=None):
221 222
    """Main program, used when run as a script.

223
    The optional 'args' argument specifies the command line to be parsed,
224 225 226 227
    defaulting to sys.argv[1:].

    The return value is an exit code to be passed to sys.exit(); it
    may be None to indicate success.
228 229 230 231

    When an exception happens during timing, a traceback is printed to
    stderr and the return value is 1.  Exceptions at other times
    (including the template compilation) are not caught.
232 233 234 235

    '_wrap_timer' is an internal interface used for unit testing.  If it
    is not None, it must be a callable that accepts a timer function
    and returns another timer function (used for unit testing).
236
    """
237 238 239 240
    if args is None:
        args = sys.argv[1:]
    import getopt
    try:
241
        opts, args = getopt.getopt(args, "n:u:s:r:tcpvh",
242
                                   ["number=", "setup=", "repeat=",
243
                                    "time", "clock", "process",
244
                                    "verbose", "unit=", "help"])
245
    except getopt.error as err:
246 247
        print(err)
        print("use -h/--help for command line help")
248 249 250 251
        return 2
    timer = default_timer
    stmt = "\n".join(args) or "pass"
    number = 0 # auto-determine
252
    setup = []
253 254
    repeat = default_repeat
    verbose = 0
255 256
    time_unit = None
    units = {"usec": 1, "msec": 1e3, "sec": 1e6}
257
    precision = 3
258 259 260 261
    for o, a in opts:
        if o in ("-n", "--number"):
            number = int(a)
        if o in ("-s", "--setup"):
262
            setup.append(a)
263 264 265 266 267 268 269
        if o in ("-u", "--unit"):
            if a in units:
                time_unit = a
            else:
                print("Unrecognized unit. Please select usec, msec, or sec.",
                    file=sys.stderr)
                return 2
270 271 272 273
        if o in ("-r", "--repeat"):
            repeat = int(a)
            if repeat <= 0:
                repeat = 1
274
        if o in ("-t", "--time"):
275
            timer = time.time
276
        if o in ("-c", "--clock"):
277
            timer = time.clock
278 279
        if o in ("-p", "--process"):
            timer = time.process_time
280 281 282 283
        if o in ("-v", "--verbose"):
            if verbose:
                precision += 1
            verbose += 1
284
        if o in ("-h", "--help"):
285
            print(__doc__, end=' ')
286
            return 0
287
    setup = "\n".join(setup) or "pass"
288 289 290 291 292
    # Include the current directory, so that local imports work (sys.path
    # contains the directory of this script, rather than the current
    # directory)
    import os
    sys.path.insert(0, os.curdir)
293 294
    if _wrap_timer is not None:
        timer = _wrap_timer(timer)
295 296 297 298 299
    t = Timer(stmt, setup, timer)
    if number == 0:
        # determine number so that 0.2 <= total time < 2.0
        for i in range(1, 10):
            number = 10**i
300 301 302 303 304
            try:
                x = t.timeit(number)
            except:
                t.print_exc()
                return 1
305
            if verbose:
306
                print("%d loops -> %.*g secs" % (number, precision, x))
307 308
            if x >= 0.2:
                break
309 310 311 312 313
    try:
        r = t.repeat(repeat, number)
    except:
        t.print_exc()
        return 1
314
    best = min(r)
315
    if verbose:
316 317
        print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
    print("%d loops," % number, end=' ')
318
    usec = best * 1e6 / number
319 320 321
    if time_unit is not None:
        print("best of %d: %.*g %s per loop" % (repeat, precision,
                                             usec/units[time_unit], time_unit))
322
    else:
323 324
        if usec < 1000:
            print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
325
        else:
326 327 328 329 330 331 332 333
            msec = usec / 1000
            if msec < 1000:
                print("best of %d: %.*g msec per loop" % (repeat,
                                                          precision, msec))
            else:
                sec = msec / 1000
                print("best of %d: %.*g sec per loop" % (repeat,
                                                         precision, sec))
334
    return None
335 336 337

if __name__ == "__main__":
    sys.exit(main())