Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: timeit.py
#! /usr/bin/env python
[0] Fix | Delete
[1] Fix | Delete
"""Tool for measuring execution time of small code snippets.
[2] Fix | Delete
[3] Fix | Delete
This module avoids a number of common traps for measuring execution
[4] Fix | Delete
times. See also Tim Peters' introduction to the Algorithms chapter in
[5] Fix | Delete
the Python Cookbook, published by O'Reilly.
[6] Fix | Delete
[7] Fix | Delete
Library usage: see the Timer class.
[8] Fix | Delete
[9] Fix | Delete
Command line usage:
[10] Fix | Delete
python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [--] [statement]
[11] Fix | Delete
[12] Fix | Delete
Options:
[13] Fix | Delete
-n/--number N: how many times to execute 'statement' (default: see below)
[14] Fix | Delete
-r/--repeat N: how many times to repeat the timer (default 3)
[15] Fix | Delete
-s/--setup S: statement to be executed once initially (default 'pass')
[16] Fix | Delete
-t/--time: use time.time() (default on Unix)
[17] Fix | Delete
-c/--clock: use time.clock() (default on Windows)
[18] Fix | Delete
-v/--verbose: print raw timing results; repeat for more digits precision
[19] Fix | Delete
-h/--help: print this usage message and exit
[20] Fix | Delete
--: separate options from statement, use when statement starts with -
[21] Fix | Delete
statement: statement to be timed (default 'pass')
[22] Fix | Delete
[23] Fix | Delete
A multi-line statement may be given by specifying each line as a
[24] Fix | Delete
separate argument; indented lines are possible by enclosing an
[25] Fix | Delete
argument in quotes and using leading spaces. Multiple -s options are
[26] Fix | Delete
treated similarly.
[27] Fix | Delete
[28] Fix | Delete
If -n is not given, a suitable number of loops is calculated by trying
[29] Fix | Delete
successive powers of 10 until the total time is at least 0.2 seconds.
[30] Fix | Delete
[31] Fix | Delete
The difference in default timer function is because on Windows,
[32] Fix | Delete
clock() has microsecond granularity but time()'s granularity is 1/60th
[33] Fix | Delete
of a second; on Unix, clock() has 1/100th of a second granularity and
[34] Fix | Delete
time() is much more precise. On either platform, the default timer
[35] Fix | Delete
functions measure wall clock time, not the CPU time. This means that
[36] Fix | Delete
other processes running on the same computer may interfere with the
[37] Fix | Delete
timing. The best thing to do when accurate timing is necessary is to
[38] Fix | Delete
repeat the timing a few times and use the best time. The -r option is
[39] Fix | Delete
good for this; the default of 3 repetitions is probably enough in most
[40] Fix | Delete
cases. On Unix, you can use clock() to measure CPU time.
[41] Fix | Delete
[42] Fix | Delete
Note: there is a certain baseline overhead associated with executing a
[43] Fix | Delete
pass statement. The code here doesn't try to hide it, but you should
[44] Fix | Delete
be aware of it. The baseline overhead can be measured by invoking the
[45] Fix | Delete
program without arguments.
[46] Fix | Delete
[47] Fix | Delete
The baseline overhead differs between Python versions! Also, to
[48] Fix | Delete
fairly compare older Python versions to Python 2.3, you may want to
[49] Fix | Delete
use python -O for the older versions to avoid timing SET_LINENO
[50] Fix | Delete
instructions.
[51] Fix | Delete
"""
[52] Fix | Delete
[53] Fix | Delete
import gc
[54] Fix | Delete
import sys
[55] Fix | Delete
import time
[56] Fix | Delete
try:
[57] Fix | Delete
import itertools
[58] Fix | Delete
except ImportError:
[59] Fix | Delete
# Must be an older Python version (see timeit() below)
[60] Fix | Delete
itertools = None
[61] Fix | Delete
[62] Fix | Delete
__all__ = ["Timer"]
[63] Fix | Delete
[64] Fix | Delete
dummy_src_name = "<timeit-src>"
[65] Fix | Delete
default_number = 1000000
[66] Fix | Delete
default_repeat = 3
[67] Fix | Delete
[68] Fix | Delete
if sys.platform == "win32":
[69] Fix | Delete
# On Windows, the best timer is time.clock()
[70] Fix | Delete
default_timer = time.clock
[71] Fix | Delete
else:
[72] Fix | Delete
# On most other platforms the best timer is time.time()
[73] Fix | Delete
default_timer = time.time
[74] Fix | Delete
[75] Fix | Delete
# Don't change the indentation of the template; the reindent() calls
[76] Fix | Delete
# in Timer.__init__() depend on setup being indented 4 spaces and stmt
[77] Fix | Delete
# being indented 8 spaces.
[78] Fix | Delete
template = """
[79] Fix | Delete
def inner(_it, _timer%(init)s):
[80] Fix | Delete
%(setup)s
[81] Fix | Delete
_t0 = _timer()
[82] Fix | Delete
for _i in _it:
[83] Fix | Delete
%(stmt)s
[84] Fix | Delete
_t1 = _timer()
[85] Fix | Delete
return _t1 - _t0
[86] Fix | Delete
"""
[87] Fix | Delete
[88] Fix | Delete
def reindent(src, indent):
[89] Fix | Delete
"""Helper to reindent a multi-line statement."""
[90] Fix | Delete
return src.replace("\n", "\n" + " "*indent)
[91] Fix | Delete
[92] Fix | Delete
def _template_func(setup, func):
[93] Fix | Delete
"""Create a timer function. Used if the "statement" is a callable."""
[94] Fix | Delete
def inner(_it, _timer, _func=func):
[95] Fix | Delete
setup()
[96] Fix | Delete
_t0 = _timer()
[97] Fix | Delete
for _i in _it:
[98] Fix | Delete
_func()
[99] Fix | Delete
_t1 = _timer()
[100] Fix | Delete
return _t1 - _t0
[101] Fix | Delete
return inner
[102] Fix | Delete
[103] Fix | Delete
class Timer:
[104] Fix | Delete
"""Class for timing execution speed of small code snippets.
[105] Fix | Delete
[106] Fix | Delete
The constructor takes a statement to be timed, an additional
[107] Fix | Delete
statement used for setup, and a timer function. Both statements
[108] Fix | Delete
default to 'pass'; the timer function is platform-dependent (see
[109] Fix | Delete
module doc string).
[110] Fix | Delete
[111] Fix | Delete
To measure the execution time of the first statement, use the
[112] Fix | Delete
timeit() method. The repeat() method is a convenience to call
[113] Fix | Delete
timeit() multiple times and return a list of results.
[114] Fix | Delete
[115] Fix | Delete
The statements may contain newlines, as long as they don't contain
[116] Fix | Delete
multi-line string literals.
[117] Fix | Delete
"""
[118] Fix | Delete
[119] Fix | Delete
def __init__(self, stmt="pass", setup="pass", timer=default_timer):
[120] Fix | Delete
"""Constructor. See class doc string."""
[121] Fix | Delete
self.timer = timer
[122] Fix | Delete
ns = {}
[123] Fix | Delete
if isinstance(stmt, basestring):
[124] Fix | Delete
# Check that the code can be compiled outside a function
[125] Fix | Delete
if isinstance(setup, basestring):
[126] Fix | Delete
compile(setup, dummy_src_name, "exec")
[127] Fix | Delete
compile(setup + '\n' + stmt, dummy_src_name, "exec")
[128] Fix | Delete
else:
[129] Fix | Delete
compile(stmt, dummy_src_name, "exec")
[130] Fix | Delete
stmt = reindent(stmt, 8)
[131] Fix | Delete
if isinstance(setup, basestring):
[132] Fix | Delete
setup = reindent(setup, 4)
[133] Fix | Delete
src = template % {'stmt': stmt, 'setup': setup, 'init': ''}
[134] Fix | Delete
elif hasattr(setup, '__call__'):
[135] Fix | Delete
src = template % {'stmt': stmt, 'setup': '_setup()',
[136] Fix | Delete
'init': ', _setup=_setup'}
[137] Fix | Delete
ns['_setup'] = setup
[138] Fix | Delete
else:
[139] Fix | Delete
raise ValueError("setup is neither a string nor callable")
[140] Fix | Delete
self.src = src # Save for traceback display
[141] Fix | Delete
code = compile(src, dummy_src_name, "exec")
[142] Fix | Delete
exec code in globals(), ns
[143] Fix | Delete
self.inner = ns["inner"]
[144] Fix | Delete
elif hasattr(stmt, '__call__'):
[145] Fix | Delete
self.src = None
[146] Fix | Delete
if isinstance(setup, basestring):
[147] Fix | Delete
_setup = setup
[148] Fix | Delete
def setup():
[149] Fix | Delete
exec _setup in globals(), ns
[150] Fix | Delete
elif not hasattr(setup, '__call__'):
[151] Fix | Delete
raise ValueError("setup is neither a string nor callable")
[152] Fix | Delete
self.inner = _template_func(setup, stmt)
[153] Fix | Delete
else:
[154] Fix | Delete
raise ValueError("stmt is neither a string nor callable")
[155] Fix | Delete
[156] Fix | Delete
def print_exc(self, file=None):
[157] Fix | Delete
"""Helper to print a traceback from the timed code.
[158] Fix | Delete
[159] Fix | Delete
Typical use:
[160] Fix | Delete
[161] Fix | Delete
t = Timer(...) # outside the try/except
[162] Fix | Delete
try:
[163] Fix | Delete
t.timeit(...) # or t.repeat(...)
[164] Fix | Delete
except:
[165] Fix | Delete
t.print_exc()
[166] Fix | Delete
[167] Fix | Delete
The advantage over the standard traceback is that source lines
[168] Fix | Delete
in the compiled template will be displayed.
[169] Fix | Delete
[170] Fix | Delete
The optional file argument directs where the traceback is
[171] Fix | Delete
sent; it defaults to sys.stderr.
[172] Fix | Delete
"""
[173] Fix | Delete
import linecache, traceback
[174] Fix | Delete
if self.src is not None:
[175] Fix | Delete
linecache.cache[dummy_src_name] = (len(self.src),
[176] Fix | Delete
None,
[177] Fix | Delete
self.src.split("\n"),
[178] Fix | Delete
dummy_src_name)
[179] Fix | Delete
# else the source is already stored somewhere else
[180] Fix | Delete
[181] Fix | Delete
traceback.print_exc(file=file)
[182] Fix | Delete
[183] Fix | Delete
def timeit(self, number=default_number):
[184] Fix | Delete
"""Time 'number' executions of the main statement.
[185] Fix | Delete
[186] Fix | Delete
To be precise, this executes the setup statement once, and
[187] Fix | Delete
then returns the time it takes to execute the main statement
[188] Fix | Delete
a number of times, as a float measured in seconds. The
[189] Fix | Delete
argument is the number of times through the loop, defaulting
[190] Fix | Delete
to one million. The main statement, the setup statement and
[191] Fix | Delete
the timer function to be used are passed to the constructor.
[192] Fix | Delete
"""
[193] Fix | Delete
if itertools:
[194] Fix | Delete
it = itertools.repeat(None, number)
[195] Fix | Delete
else:
[196] Fix | Delete
it = [None] * number
[197] Fix | Delete
gcold = gc.isenabled()
[198] Fix | Delete
gc.disable()
[199] Fix | Delete
try:
[200] Fix | Delete
timing = self.inner(it, self.timer)
[201] Fix | Delete
finally:
[202] Fix | Delete
if gcold:
[203] Fix | Delete
gc.enable()
[204] Fix | Delete
return timing
[205] Fix | Delete
[206] Fix | Delete
def repeat(self, repeat=default_repeat, number=default_number):
[207] Fix | Delete
"""Call timeit() a few times.
[208] Fix | Delete
[209] Fix | Delete
This is a convenience function that calls the timeit()
[210] Fix | Delete
repeatedly, returning a list of results. The first argument
[211] Fix | Delete
specifies how many times to call timeit(), defaulting to 3;
[212] Fix | Delete
the second argument specifies the timer argument, defaulting
[213] Fix | Delete
to one million.
[214] Fix | Delete
[215] Fix | Delete
Note: it's tempting to calculate mean and standard deviation
[216] Fix | Delete
from the result vector and report these. However, this is not
[217] Fix | Delete
very useful. In a typical case, the lowest value gives a
[218] Fix | Delete
lower bound for how fast your machine can run the given code
[219] Fix | Delete
snippet; higher values in the result vector are typically not
[220] Fix | Delete
caused by variability in Python's speed, but by other
[221] Fix | Delete
processes interfering with your timing accuracy. So the min()
[222] Fix | Delete
of the result is probably the only number you should be
[223] Fix | Delete
interested in. After that, you should look at the entire
[224] Fix | Delete
vector and apply common sense rather than statistics.
[225] Fix | Delete
"""
[226] Fix | Delete
r = []
[227] Fix | Delete
for i in range(repeat):
[228] Fix | Delete
t = self.timeit(number)
[229] Fix | Delete
r.append(t)
[230] Fix | Delete
return r
[231] Fix | Delete
[232] Fix | Delete
def timeit(stmt="pass", setup="pass", timer=default_timer,
[233] Fix | Delete
number=default_number):
[234] Fix | Delete
"""Convenience function to create Timer object and call timeit method."""
[235] Fix | Delete
return Timer(stmt, setup, timer).timeit(number)
[236] Fix | Delete
[237] Fix | Delete
def repeat(stmt="pass", setup="pass", timer=default_timer,
[238] Fix | Delete
repeat=default_repeat, number=default_number):
[239] Fix | Delete
"""Convenience function to create Timer object and call repeat method."""
[240] Fix | Delete
return Timer(stmt, setup, timer).repeat(repeat, number)
[241] Fix | Delete
[242] Fix | Delete
def main(args=None, _wrap_timer=None):
[243] Fix | Delete
"""Main program, used when run as a script.
[244] Fix | Delete
[245] Fix | Delete
The optional 'args' argument specifies the command line to be parsed,
[246] Fix | Delete
defaulting to sys.argv[1:].
[247] Fix | Delete
[248] Fix | Delete
The return value is an exit code to be passed to sys.exit(); it
[249] Fix | Delete
may be None to indicate success.
[250] Fix | Delete
[251] Fix | Delete
When an exception happens during timing, a traceback is printed to
[252] Fix | Delete
stderr and the return value is 1. Exceptions at other times
[253] Fix | Delete
(including the template compilation) are not caught.
[254] Fix | Delete
[255] Fix | Delete
'_wrap_timer' is an internal interface used for unit testing. If it
[256] Fix | Delete
is not None, it must be a callable that accepts a timer function
[257] Fix | Delete
and returns another timer function (used for unit testing).
[258] Fix | Delete
"""
[259] Fix | Delete
if args is None:
[260] Fix | Delete
args = sys.argv[1:]
[261] Fix | Delete
import getopt
[262] Fix | Delete
try:
[263] Fix | Delete
opts, args = getopt.getopt(args, "n:s:r:tcvh",
[264] Fix | Delete
["number=", "setup=", "repeat=",
[265] Fix | Delete
"time", "clock", "verbose", "help"])
[266] Fix | Delete
except getopt.error, err:
[267] Fix | Delete
print err
[268] Fix | Delete
print "use -h/--help for command line help"
[269] Fix | Delete
return 2
[270] Fix | Delete
timer = default_timer
[271] Fix | Delete
stmt = "\n".join(args) or "pass"
[272] Fix | Delete
number = 0 # auto-determine
[273] Fix | Delete
setup = []
[274] Fix | Delete
repeat = default_repeat
[275] Fix | Delete
verbose = 0
[276] Fix | Delete
precision = 3
[277] Fix | Delete
for o, a in opts:
[278] Fix | Delete
if o in ("-n", "--number"):
[279] Fix | Delete
number = int(a)
[280] Fix | Delete
if o in ("-s", "--setup"):
[281] Fix | Delete
setup.append(a)
[282] Fix | Delete
if o in ("-r", "--repeat"):
[283] Fix | Delete
repeat = int(a)
[284] Fix | Delete
if repeat <= 0:
[285] Fix | Delete
repeat = 1
[286] Fix | Delete
if o in ("-t", "--time"):
[287] Fix | Delete
timer = time.time
[288] Fix | Delete
if o in ("-c", "--clock"):
[289] Fix | Delete
timer = time.clock
[290] Fix | Delete
if o in ("-v", "--verbose"):
[291] Fix | Delete
if verbose:
[292] Fix | Delete
precision += 1
[293] Fix | Delete
verbose += 1
[294] Fix | Delete
if o in ("-h", "--help"):
[295] Fix | Delete
print __doc__,
[296] Fix | Delete
return 0
[297] Fix | Delete
setup = "\n".join(setup) or "pass"
[298] Fix | Delete
# Include the current directory, so that local imports work (sys.path
[299] Fix | Delete
# contains the directory of this script, rather than the current
[300] Fix | Delete
# directory)
[301] Fix | Delete
import os
[302] Fix | Delete
sys.path.insert(0, os.curdir)
[303] Fix | Delete
if _wrap_timer is not None:
[304] Fix | Delete
timer = _wrap_timer(timer)
[305] Fix | Delete
t = Timer(stmt, setup, timer)
[306] Fix | Delete
if number == 0:
[307] Fix | Delete
# determine number so that 0.2 <= total time < 2.0
[308] Fix | Delete
for i in range(1, 10):
[309] Fix | Delete
number = 10**i
[310] Fix | Delete
try:
[311] Fix | Delete
x = t.timeit(number)
[312] Fix | Delete
except:
[313] Fix | Delete
t.print_exc()
[314] Fix | Delete
return 1
[315] Fix | Delete
if verbose:
[316] Fix | Delete
print "%d loops -> %.*g secs" % (number, precision, x)
[317] Fix | Delete
if x >= 0.2:
[318] Fix | Delete
break
[319] Fix | Delete
try:
[320] Fix | Delete
r = t.repeat(repeat, number)
[321] Fix | Delete
except:
[322] Fix | Delete
t.print_exc()
[323] Fix | Delete
return 1
[324] Fix | Delete
best = min(r)
[325] Fix | Delete
if verbose:
[326] Fix | Delete
print "raw times:", " ".join(["%.*g" % (precision, x) for x in r])
[327] Fix | Delete
print "%d loops," % number,
[328] Fix | Delete
usec = best * 1e6 / number
[329] Fix | Delete
if usec < 1000:
[330] Fix | Delete
print "best of %d: %.*g usec per loop" % (repeat, precision, usec)
[331] Fix | Delete
else:
[332] Fix | Delete
msec = usec / 1000
[333] Fix | Delete
if msec < 1000:
[334] Fix | Delete
print "best of %d: %.*g msec per loop" % (repeat, precision, msec)
[335] Fix | Delete
else:
[336] Fix | Delete
sec = msec / 1000
[337] Fix | Delete
print "best of %d: %.*g sec per loop" % (repeat, precision, sec)
[338] Fix | Delete
return None
[339] Fix | Delete
[340] Fix | Delete
if __name__ == "__main__":
[341] Fix | Delete
sys.exit(main())
[342] Fix | Delete
[343] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function