Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
File: timeit.py
#! /usr/bin/python3.8
[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] [-p] [-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 5)
[15] Fix | Delete
-s/--setup S: statement to be executed once initially (default 'pass').
[16] Fix | Delete
Execution time of this setup statement is NOT timed.
[17] Fix | Delete
-p/--process: use time.process_time() (default is time.perf_counter())
[18] Fix | Delete
-v/--verbose: print raw timing results; repeat for more digits precision
[19] Fix | Delete
-u/--unit: set the output time unit (nsec, usec, msec, or sec)
[20] Fix | Delete
-h/--help: print this usage message and exit
[21] Fix | Delete
--: separate options from statement, use when statement starts with -
[22] Fix | Delete
statement: statement to be timed (default 'pass')
[23] Fix | Delete
[24] Fix | Delete
A multi-line statement may be given by specifying each line as a
[25] Fix | Delete
separate argument; indented lines are possible by enclosing an
[26] Fix | Delete
argument in quotes and using leading spaces. Multiple -s options are
[27] Fix | Delete
treated similarly.
[28] Fix | Delete
[29] Fix | Delete
If -n is not given, a suitable number of loops is calculated by trying
[30] Fix | Delete
increasing numbers from the sequence 1, 2, 5, 10, 20, 50, ... until the
[31] Fix | Delete
total time is at least 0.2 seconds.
[32] Fix | Delete
[33] Fix | Delete
Note: there is a certain baseline overhead associated with executing a
[34] Fix | Delete
pass statement. It differs between versions. The code here doesn't try
[35] Fix | Delete
to hide it, but you should be aware of it. The baseline overhead can be
[36] Fix | Delete
measured by invoking the program without arguments.
[37] Fix | Delete
[38] Fix | Delete
Classes:
[39] Fix | Delete
[40] Fix | Delete
Timer
[41] Fix | Delete
[42] Fix | Delete
Functions:
[43] Fix | Delete
[44] Fix | Delete
timeit(string, string) -> float
[45] Fix | Delete
repeat(string, string) -> list
[46] Fix | Delete
default_timer() -> float
[47] Fix | Delete
[48] Fix | Delete
"""
[49] Fix | Delete
[50] Fix | Delete
import gc
[51] Fix | Delete
import sys
[52] Fix | Delete
import time
[53] Fix | Delete
import itertools
[54] Fix | Delete
[55] Fix | Delete
__all__ = ["Timer", "timeit", "repeat", "default_timer"]
[56] Fix | Delete
[57] Fix | Delete
dummy_src_name = "<timeit-src>"
[58] Fix | Delete
default_number = 1000000
[59] Fix | Delete
default_repeat = 5
[60] Fix | Delete
default_timer = time.perf_counter
[61] Fix | Delete
[62] Fix | Delete
_globals = globals
[63] Fix | Delete
[64] Fix | Delete
# Don't change the indentation of the template; the reindent() calls
[65] Fix | Delete
# in Timer.__init__() depend on setup being indented 4 spaces and stmt
[66] Fix | Delete
# being indented 8 spaces.
[67] Fix | Delete
template = """
[68] Fix | Delete
def inner(_it, _timer{init}):
[69] Fix | Delete
{setup}
[70] Fix | Delete
_t0 = _timer()
[71] Fix | Delete
for _i in _it:
[72] Fix | Delete
{stmt}
[73] Fix | Delete
_t1 = _timer()
[74] Fix | Delete
return _t1 - _t0
[75] Fix | Delete
"""
[76] Fix | Delete
[77] Fix | Delete
def reindent(src, indent):
[78] Fix | Delete
"""Helper to reindent a multi-line statement."""
[79] Fix | Delete
return src.replace("\n", "\n" + " "*indent)
[80] Fix | Delete
[81] Fix | Delete
class Timer:
[82] Fix | Delete
"""Class for timing execution speed of small code snippets.
[83] Fix | Delete
[84] Fix | Delete
The constructor takes a statement to be timed, an additional
[85] Fix | Delete
statement used for setup, and a timer function. Both statements
[86] Fix | Delete
default to 'pass'; the timer function is platform-dependent (see
[87] Fix | Delete
module doc string). If 'globals' is specified, the code will be
[88] Fix | Delete
executed within that namespace (as opposed to inside timeit's
[89] Fix | Delete
namespace).
[90] Fix | Delete
[91] Fix | Delete
To measure the execution time of the first statement, use the
[92] Fix | Delete
timeit() method. The repeat() method is a convenience to call
[93] Fix | Delete
timeit() multiple times and return a list of results.
[94] Fix | Delete
[95] Fix | Delete
The statements may contain newlines, as long as they don't contain
[96] Fix | Delete
multi-line string literals.
[97] Fix | Delete
"""
[98] Fix | Delete
[99] Fix | Delete
def __init__(self, stmt="pass", setup="pass", timer=default_timer,
[100] Fix | Delete
globals=None):
[101] Fix | Delete
"""Constructor. See class doc string."""
[102] Fix | Delete
self.timer = timer
[103] Fix | Delete
local_ns = {}
[104] Fix | Delete
global_ns = _globals() if globals is None else globals
[105] Fix | Delete
init = ''
[106] Fix | Delete
if isinstance(setup, str):
[107] Fix | Delete
# Check that the code can be compiled outside a function
[108] Fix | Delete
compile(setup, dummy_src_name, "exec")
[109] Fix | Delete
stmtprefix = setup + '\n'
[110] Fix | Delete
setup = reindent(setup, 4)
[111] Fix | Delete
elif callable(setup):
[112] Fix | Delete
local_ns['_setup'] = setup
[113] Fix | Delete
init += ', _setup=_setup'
[114] Fix | Delete
stmtprefix = ''
[115] Fix | Delete
setup = '_setup()'
[116] Fix | Delete
else:
[117] Fix | Delete
raise ValueError("setup is neither a string nor callable")
[118] Fix | Delete
if isinstance(stmt, str):
[119] Fix | Delete
# Check that the code can be compiled outside a function
[120] Fix | Delete
compile(stmtprefix + stmt, dummy_src_name, "exec")
[121] Fix | Delete
stmt = reindent(stmt, 8)
[122] Fix | Delete
elif callable(stmt):
[123] Fix | Delete
local_ns['_stmt'] = stmt
[124] Fix | Delete
init += ', _stmt=_stmt'
[125] Fix | Delete
stmt = '_stmt()'
[126] Fix | Delete
else:
[127] Fix | Delete
raise ValueError("stmt is neither a string nor callable")
[128] Fix | Delete
src = template.format(stmt=stmt, setup=setup, init=init)
[129] Fix | Delete
self.src = src # Save for traceback display
[130] Fix | Delete
code = compile(src, dummy_src_name, "exec")
[131] Fix | Delete
exec(code, global_ns, local_ns)
[132] Fix | Delete
self.inner = local_ns["inner"]
[133] Fix | Delete
[134] Fix | Delete
def print_exc(self, file=None):
[135] Fix | Delete
"""Helper to print a traceback from the timed code.
[136] Fix | Delete
[137] Fix | Delete
Typical use:
[138] Fix | Delete
[139] Fix | Delete
t = Timer(...) # outside the try/except
[140] Fix | Delete
try:
[141] Fix | Delete
t.timeit(...) # or t.repeat(...)
[142] Fix | Delete
except:
[143] Fix | Delete
t.print_exc()
[144] Fix | Delete
[145] Fix | Delete
The advantage over the standard traceback is that source lines
[146] Fix | Delete
in the compiled template will be displayed.
[147] Fix | Delete
[148] Fix | Delete
The optional file argument directs where the traceback is
[149] Fix | Delete
sent; it defaults to sys.stderr.
[150] Fix | Delete
"""
[151] Fix | Delete
import linecache, traceback
[152] Fix | Delete
if self.src is not None:
[153] Fix | Delete
linecache.cache[dummy_src_name] = (len(self.src),
[154] Fix | Delete
None,
[155] Fix | Delete
self.src.split("\n"),
[156] Fix | Delete
dummy_src_name)
[157] Fix | Delete
# else the source is already stored somewhere else
[158] Fix | Delete
[159] Fix | Delete
traceback.print_exc(file=file)
[160] Fix | Delete
[161] Fix | Delete
def timeit(self, number=default_number):
[162] Fix | Delete
"""Time 'number' executions of the main statement.
[163] Fix | Delete
[164] Fix | Delete
To be precise, this executes the setup statement once, and
[165] Fix | Delete
then returns the time it takes to execute the main statement
[166] Fix | Delete
a number of times, as a float measured in seconds. The
[167] Fix | Delete
argument is the number of times through the loop, defaulting
[168] Fix | Delete
to one million. The main statement, the setup statement and
[169] Fix | Delete
the timer function to be used are passed to the constructor.
[170] Fix | Delete
"""
[171] Fix | Delete
it = itertools.repeat(None, number)
[172] Fix | Delete
gcold = gc.isenabled()
[173] Fix | Delete
gc.disable()
[174] Fix | Delete
try:
[175] Fix | Delete
timing = self.inner(it, self.timer)
[176] Fix | Delete
finally:
[177] Fix | Delete
if gcold:
[178] Fix | Delete
gc.enable()
[179] Fix | Delete
return timing
[180] Fix | Delete
[181] Fix | Delete
def repeat(self, repeat=default_repeat, number=default_number):
[182] Fix | Delete
"""Call timeit() a few times.
[183] Fix | Delete
[184] Fix | Delete
This is a convenience function that calls the timeit()
[185] Fix | Delete
repeatedly, returning a list of results. The first argument
[186] Fix | Delete
specifies how many times to call timeit(), defaulting to 5;
[187] Fix | Delete
the second argument specifies the timer argument, defaulting
[188] Fix | Delete
to one million.
[189] Fix | Delete
[190] Fix | Delete
Note: it's tempting to calculate mean and standard deviation
[191] Fix | Delete
from the result vector and report these. However, this is not
[192] Fix | Delete
very useful. In a typical case, the lowest value gives a
[193] Fix | Delete
lower bound for how fast your machine can run the given code
[194] Fix | Delete
snippet; higher values in the result vector are typically not
[195] Fix | Delete
caused by variability in Python's speed, but by other
[196] Fix | Delete
processes interfering with your timing accuracy. So the min()
[197] Fix | Delete
of the result is probably the only number you should be
[198] Fix | Delete
interested in. After that, you should look at the entire
[199] Fix | Delete
vector and apply common sense rather than statistics.
[200] Fix | Delete
"""
[201] Fix | Delete
r = []
[202] Fix | Delete
for i in range(repeat):
[203] Fix | Delete
t = self.timeit(number)
[204] Fix | Delete
r.append(t)
[205] Fix | Delete
return r
[206] Fix | Delete
[207] Fix | Delete
def autorange(self, callback=None):
[208] Fix | Delete
"""Return the number of loops and time taken so that total time >= 0.2.
[209] Fix | Delete
[210] Fix | Delete
Calls the timeit method with increasing numbers from the sequence
[211] Fix | Delete
1, 2, 5, 10, 20, 50, ... until the time taken is at least 0.2
[212] Fix | Delete
second. Returns (number, time_taken).
[213] Fix | Delete
[214] Fix | Delete
If *callback* is given and is not None, it will be called after
[215] Fix | Delete
each trial with two arguments: ``callback(number, time_taken)``.
[216] Fix | Delete
"""
[217] Fix | Delete
i = 1
[218] Fix | Delete
while True:
[219] Fix | Delete
for j in 1, 2, 5:
[220] Fix | Delete
number = i * j
[221] Fix | Delete
time_taken = self.timeit(number)
[222] Fix | Delete
if callback:
[223] Fix | Delete
callback(number, time_taken)
[224] Fix | Delete
if time_taken >= 0.2:
[225] Fix | Delete
return (number, time_taken)
[226] Fix | Delete
i *= 10
[227] Fix | Delete
[228] Fix | Delete
def timeit(stmt="pass", setup="pass", timer=default_timer,
[229] Fix | Delete
number=default_number, globals=None):
[230] Fix | Delete
"""Convenience function to create Timer object and call timeit method."""
[231] Fix | Delete
return Timer(stmt, setup, timer, globals).timeit(number)
[232] Fix | Delete
[233] Fix | Delete
def repeat(stmt="pass", setup="pass", timer=default_timer,
[234] Fix | Delete
repeat=default_repeat, number=default_number, globals=None):
[235] Fix | Delete
"""Convenience function to create Timer object and call repeat method."""
[236] Fix | Delete
return Timer(stmt, setup, timer, globals).repeat(repeat, number)
[237] Fix | Delete
[238] Fix | Delete
def main(args=None, *, _wrap_timer=None):
[239] Fix | Delete
"""Main program, used when run as a script.
[240] Fix | Delete
[241] Fix | Delete
The optional 'args' argument specifies the command line to be parsed,
[242] Fix | Delete
defaulting to sys.argv[1:].
[243] Fix | Delete
[244] Fix | Delete
The return value is an exit code to be passed to sys.exit(); it
[245] Fix | Delete
may be None to indicate success.
[246] Fix | Delete
[247] Fix | Delete
When an exception happens during timing, a traceback is printed to
[248] Fix | Delete
stderr and the return value is 1. Exceptions at other times
[249] Fix | Delete
(including the template compilation) are not caught.
[250] Fix | Delete
[251] Fix | Delete
'_wrap_timer' is an internal interface used for unit testing. If it
[252] Fix | Delete
is not None, it must be a callable that accepts a timer function
[253] Fix | Delete
and returns another timer function (used for unit testing).
[254] Fix | Delete
"""
[255] Fix | Delete
if args is None:
[256] Fix | Delete
args = sys.argv[1:]
[257] Fix | Delete
import getopt
[258] Fix | Delete
try:
[259] Fix | Delete
opts, args = getopt.getopt(args, "n:u:s:r:tcpvh",
[260] Fix | Delete
["number=", "setup=", "repeat=",
[261] Fix | Delete
"time", "clock", "process",
[262] Fix | Delete
"verbose", "unit=", "help"])
[263] Fix | Delete
except getopt.error as err:
[264] Fix | Delete
print(err)
[265] Fix | Delete
print("use -h/--help for command line help")
[266] Fix | Delete
return 2
[267] Fix | Delete
[268] Fix | Delete
timer = default_timer
[269] Fix | Delete
stmt = "\n".join(args) or "pass"
[270] Fix | Delete
number = 0 # auto-determine
[271] Fix | Delete
setup = []
[272] Fix | Delete
repeat = default_repeat
[273] Fix | Delete
verbose = 0
[274] Fix | Delete
time_unit = None
[275] Fix | Delete
units = {"nsec": 1e-9, "usec": 1e-6, "msec": 1e-3, "sec": 1.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 ("-u", "--unit"):
[283] Fix | Delete
if a in units:
[284] Fix | Delete
time_unit = a
[285] Fix | Delete
else:
[286] Fix | Delete
print("Unrecognized unit. Please select nsec, usec, msec, or sec.",
[287] Fix | Delete
file=sys.stderr)
[288] Fix | Delete
return 2
[289] Fix | Delete
if o in ("-r", "--repeat"):
[290] Fix | Delete
repeat = int(a)
[291] Fix | Delete
if repeat <= 0:
[292] Fix | Delete
repeat = 1
[293] Fix | Delete
if o in ("-p", "--process"):
[294] Fix | Delete
timer = time.process_time
[295] Fix | Delete
if o in ("-v", "--verbose"):
[296] Fix | Delete
if verbose:
[297] Fix | Delete
precision += 1
[298] Fix | Delete
verbose += 1
[299] Fix | Delete
if o in ("-h", "--help"):
[300] Fix | Delete
print(__doc__, end=' ')
[301] Fix | Delete
return 0
[302] Fix | Delete
setup = "\n".join(setup) or "pass"
[303] Fix | Delete
[304] Fix | Delete
# Include the current directory, so that local imports work (sys.path
[305] Fix | Delete
# contains the directory of this script, rather than the current
[306] Fix | Delete
# directory)
[307] Fix | Delete
import os
[308] Fix | Delete
sys.path.insert(0, os.curdir)
[309] Fix | Delete
if _wrap_timer is not None:
[310] Fix | Delete
timer = _wrap_timer(timer)
[311] Fix | Delete
[312] Fix | Delete
t = Timer(stmt, setup, timer)
[313] Fix | Delete
if number == 0:
[314] Fix | Delete
# determine number so that 0.2 <= total time < 2.0
[315] Fix | Delete
callback = None
[316] Fix | Delete
if verbose:
[317] Fix | Delete
def callback(number, time_taken):
[318] Fix | Delete
msg = "{num} loop{s} -> {secs:.{prec}g} secs"
[319] Fix | Delete
plural = (number != 1)
[320] Fix | Delete
print(msg.format(num=number, s='s' if plural else '',
[321] Fix | Delete
secs=time_taken, prec=precision))
[322] Fix | Delete
try:
[323] Fix | Delete
number, _ = t.autorange(callback)
[324] Fix | Delete
except:
[325] Fix | Delete
t.print_exc()
[326] Fix | Delete
return 1
[327] Fix | Delete
[328] Fix | Delete
if verbose:
[329] Fix | Delete
print()
[330] Fix | Delete
[331] Fix | Delete
try:
[332] Fix | Delete
raw_timings = t.repeat(repeat, number)
[333] Fix | Delete
except:
[334] Fix | Delete
t.print_exc()
[335] Fix | Delete
return 1
[336] Fix | Delete
[337] Fix | Delete
def format_time(dt):
[338] Fix | Delete
unit = time_unit
[339] Fix | Delete
[340] Fix | Delete
if unit is not None:
[341] Fix | Delete
scale = units[unit]
[342] Fix | Delete
else:
[343] Fix | Delete
scales = [(scale, unit) for unit, scale in units.items()]
[344] Fix | Delete
scales.sort(reverse=True)
[345] Fix | Delete
for scale, unit in scales:
[346] Fix | Delete
if dt >= scale:
[347] Fix | Delete
break
[348] Fix | Delete
[349] Fix | Delete
return "%.*g %s" % (precision, dt / scale, unit)
[350] Fix | Delete
[351] Fix | Delete
if verbose:
[352] Fix | Delete
print("raw times: %s" % ", ".join(map(format_time, raw_timings)))
[353] Fix | Delete
print()
[354] Fix | Delete
timings = [dt / number for dt in raw_timings]
[355] Fix | Delete
[356] Fix | Delete
best = min(timings)
[357] Fix | Delete
print("%d loop%s, best of %d: %s per loop"
[358] Fix | Delete
% (number, 's' if number != 1 else '',
[359] Fix | Delete
repeat, format_time(best)))
[360] Fix | Delete
[361] Fix | Delete
best = min(timings)
[362] Fix | Delete
worst = max(timings)
[363] Fix | Delete
if worst >= best * 4:
[364] Fix | Delete
import warnings
[365] Fix | Delete
warnings.warn_explicit("The test results are likely unreliable. "
[366] Fix | Delete
"The worst time (%s) was more than four times "
[367] Fix | Delete
"slower than the best time (%s)."
[368] Fix | Delete
% (format_time(worst), format_time(best)),
[369] Fix | Delete
UserWarning, '', 0)
[370] Fix | Delete
return None
[371] Fix | Delete
[372] Fix | Delete
if __name__ == "__main__":
[373] Fix | Delete
sys.exit(main())
[374] Fix | Delete
[375] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function