5 # A script for timing snippets of Python code.
7 # By default, this script will execute a single Python file specified on
8 # the command line and time any functions in a list named "FunctionList"
9 # set by the Python file under test, or (by default) time any functions
10 # in the file whose names begin with "Func".
12 # All functions are assumed to get passed the same arguments, and the
13 # inputs are specified in a list named "Data," each element of which
14 # is a list consisting of a tag name, a list of positional arguments,
15 # and a dictionary of keyword arguments.
17 # Each function is expected to test a single, comparable snippet of
18 # of Python code. IMPORTANT: We want to test the timing of the code
19 # itself, not Python function call overhead, so every function should
20 # put its code under test within the following block:
22 # for i in IterationList:
24 # This will allow (as much as possible) us to time just the code itself,
25 # not Python function call overhead.
26 from __future__ import generators ### KEEP FOR COMPATIBILITY FIXERS
34 Usage: bench.py OPTIONS file.py
35 --clock Use the time.clock function
36 --func PREFIX Test functions whose names begin with PREFIX
37 -h, --help Display this help and exit
38 -i ITER, --iterations ITER Run each code snippet ITER times
39 --time Use the time.time function
40 -r RUNS, --runs RUNS Average times for RUNS invocations of
43 # How many times each snippet of code will be (or should be) run by the
44 # functions under test to gather the time (the "inner loop").
48 # How many times we'll run each function to collect its aggregate time
49 # and try to average out timing differences induced by system performance
54 # The prefix of the functions under test. This will be used if
55 # there's no explicit list defined in FunctionList.
57 FunctionPrefix = 'Func'
59 # The function used to get the current time. The default of time.time is
60 # good on most UNIX systems, but time.clock (selectable via the --clock
61 # option) is better on Windows and some other UNIX systems.
66 opts, args = getopt.getopt(sys.argv[1:], 'hi:r:',
67 ['clock', 'func=', 'help',
68 'iterations=', 'time', 'runs='])
75 elif o in ['-h', '--help']:
76 sys.stdout.write(Usage)
78 elif o in ['-i', '--iterations']:
82 elif o in ['-r', '--runs']:
86 sys.stderr.write("bench.py: only one file argument must be specified\n")
87 sys.stderr.write(Usage)
91 exec(open(args[0], 'rU').read())
97 function_names = sorted([x for x in locals().keys() if x[:4] == FunctionPrefix])
98 l = [locals()[f] for f in function_names]
99 FunctionList = [f for f in l if isinstance(f, types.FunctionType)]
101 IterationList = [None] * Iterations
103 def timer(func, *args, **kw):
105 for i in range(Runs):
109 results.append((finish - start) / Iterations)
112 def display(label, results):
113 total = reduce(lambda x, y: x+y, results, 0.0)
114 print " %8.3f" % ((total * 1e6) / len(results)), ':', label
116 for func in FunctionList:
117 if func.__doc__: d = ' (' + func.__doc__ + ')'
119 print func.__name__ + d + ':'
121 for label, args, kw in Data:
122 r = timer(func, *args, **kw)
127 # indent-tabs-mode:nil
129 # vim: set expandtab tabstop=4 shiftwidth=4: