本文整理汇总了Python中timeit.Timer.repeat方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.repeat方法的具体用法?Python Timer.repeat怎么用?Python Timer.repeat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类timeit.Timer
的用法示例。
在下文中一共展示了Timer.repeat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fbenchmark
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def fbenchmark(f, var=[Symbol("x")]):
"""
Do some benchmarks with f using clambdify, lambdify and psyco.
"""
global cf, pf, psyf
start = time()
cf = clambdify(var, f)
print "compile time (including sympy overhead): %f s" % (time() - start)
pf = lambdify(var, f, "math")
psyf = None
try:
import psyco
psyf = lambdify(var, f, "math")
psyco.bind(psyf)
except ImportError:
pass
code = """for x in (i/1000. for i in range(1000)):
f(%s)""" % (
"x," * len(var)
).rstrip(
","
)
t1 = Timer(code, "from __main__ import cf as f")
t2 = Timer(code, "from __main__ import pf as f")
if psyf:
t3 = Timer(code, "from __main__ import psyf as f")
else:
t3 = None
print "for x = (0, 1, 2, ..., 999)/1000"
print "20 times in 3 runs"
print "compiled: %.4f %.4f %.4f" % tuple(t1.repeat(3, 20))
print "Python lambda: %.4f %.4f %.4f" % tuple(t2.repeat(3, 20))
if t3:
print "Psyco lambda: %.4f %.4f %.4f" % tuple(t3.repeat(3, 20))
示例2: fbenchmark
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def fbenchmark(f, var=[Symbol('x')]):
"""
Do some benchmarks with f using clambdify, lambdify and psyco.
"""
global cf, pf, psyf
start = time()
cf = clambdify(var, f)
print('compile time (including sympy overhead): %f s' % (
time() - start))
pf = lambdify(var, f, 'math')
psyf = None
psyco = import_module('psyco')
if psyco:
psyf = lambdify(var, f, 'math')
psyco.bind(psyf)
code = '''for x in (i/1000. for i in range(1000)):
f(%s)''' % ('x,'*len(var)).rstrip(',')
t1 = Timer(code, 'from __main__ import cf as f')
t2 = Timer(code, 'from __main__ import pf as f')
if psyf:
t3 = Timer(code, 'from __main__ import psyf as f')
else:
t3 = None
print('for x = (0, 1, 2, ..., 999)/1000')
print('20 times in 3 runs')
print('compiled: %.4f %.4f %.4f' % tuple(t1.repeat(3, 20)))
print('Python lambda: %.4f %.4f %.4f' % tuple(t2.repeat(3, 20)))
if t3:
print('Psyco lambda: %.4f %.4f %.4f' % tuple(t3.repeat(3, 20)))
示例3: test3b_timing_calc
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def test3b_timing_calc(self):
"""Test Zernike calculation performance with and without cache, print results"""
t1 = Timer("""
a=calc_zernike(vec, rad, z_cache)
""", """
from zern import calc_zern_basis, fit_zernike, calc_zernike
import numpy as np
rad = %d
nmodes = %d
vec = np.random.random(nmodes)
z_cache = calc_zern_basis(len(vec), rad)
""" % (self.rad, self.nmodes) )
t2 = Timer("""
a=calc_zernike(vec, rad, {})
""", """
from zern import calc_zern_basis, fit_zernike, calc_zernike
import numpy as np
rad = %d
nmodes = %d
vec = np.random.random(nmodes)
""" % (self.rad, self.nmodes) )
t_cached = min(t1.repeat(2, self.calc_iter))/self.calc_iter
t_nocache = min(t2.repeat(2, self.calc_iter))/self.calc_iter
print "test3b_timing_calc(): rad=257, nmodes=25 cache: %.3g s/it no cache: %.3g s/it" % (t_cached, t_nocache)
示例4: test2a_timing
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def test2a_timing(self):
"""Test timing for two functions"""
print "test2a_timing(): timings in msec/iter"
for sz in self.sz_l[:1]:
for spsz in self.spotsz_l:
for pos in self.pos_l:
setup_str = """
from __main__ import gauss, _gauss_slow
import numpy as np
sz = (%d,%d)
spsz = %g
pos = (%d,%d)
amp = %g
noi = %g
""" % (
sz + (spsz,) + pos + (self.amp, self.noi)
)
t1 = Timer(
"""
g=_gauss_slow(sz, spsz, pos, amp, noi)
""",
setup_str,
)
t2 = Timer(
"""
a=gauss(sz, spsz, pos, amp, noi)
""",
setup_str,
)
t_g1 = 1000 * min(t1.repeat(3, self.niter)) / self.niter
t_g2 = 1000 * min(t2.repeat(3, self.niter)) / self.niter
print "test2a_timing(): sz:", sz, "g1: %.3g, g2: %.3g, speedup: %.3g" % (t_g1, t_g2, t_g1 / t_g2)
示例5: _timeit
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def _timeit(*args, **kwargs):
if repeat:
t = Timer(partial(func, *args, **kwargs))
print 'hello'
try:
print t.repeat(repeat, number)
except TypeError, e:
print 'timing decorator: %s' % e
示例6: evaluateRunTime
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def evaluateRunTime():
global SDStr
from timeit import Timer
for SDL in SDStr:
print( SDL)
t1 = Timer("Shudu(\"%s\").scanSDL()" % SDL, "from __main__ import Shudu")
print( sum(t1.repeat(10, 1))/10)
print( "==================================")
for SDL in SDStr:
SDL.replace("0", ".")
print( SDL)
t1 = Timer("solve(\"%s\")" % SDL, "from sudoku import solve")
print( sum(t1.repeat(10, 1))/10)
示例7: time_me
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def time_me(self, gc, tst, params):
""" Time the test for class gc and its comparison TimingClass """
stmt, t_setup, runs, reps = params
#
setup = "import networkx as NX\nG=NX.%s()\n" % gc + t_setup
G = eval("nx." + gc + "()")
cc = graph_type[(G.is_directed(), G.is_multigraph())]
compare_setup = ("import networkx as NX\n" "import timingclasses as tc\n" "G=tc.%s()\n" % (cc,)) + t_setup
#
tgc = Timer(stmt, setup)
tcc = Timer(stmt, compare_setup)
#
t = tgc.repeat(repeat=runs, number=reps)
bt = tcc.repeat(repeat=runs, number=reps)
return min(t), min(bt)
示例8: get_closest_region
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def get_closest_region(service="ec2", repetitions=1):
"""
Get the closest region for a particular service based on its average response time.
:type service: str
:param service: The service to attempt a connection to. By default, this is ``ec2``.
:type repetitions: int
:param repetitions: The number of measurements to take before calculating an average.
"""
regions = [
region.name
for region in regioninfo.get_regions(service)
if "gov" not in region.name and "cn" not in region.name
]
latency = {}
for region in regions:
connection = Timer(
"h.request('GET', '/')",
"from http.client import HTTPSConnection; h=HTTPSConnection('%s.%s.amazonaws.com')" % (service, region),
)
times = connection.repeat(repetitions, 1)
avg_latency = sum(times) / float(len(times))
latency[region] = avg_latency
logger.info("Average latency to Amazon %s %s is %s" % (service.upper(), region, latency[region]))
region = min(latency, key=latency.get)
return region
示例9: time_query_using_module
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def time_query_using_module(module):
# This is largely copied verbatim from the 'timeit' module
repeat = 3
number = 10
verbose = True
precision = 3
stmt = partial(query_using_greenlets, module)
t = Timer(stmt)
try:
r = t.repeat(repeat, number)
except:
t.print_exc()
return 1
best = min(r)
if verbose:
print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
print("%s: %d loops," % (module.__name__, number))
usec = best * 1e6 / number
if usec < 1000:
print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
else:
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))
示例10: evaluateRunTime
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def evaluateRunTime():
global SDStr
from timeit import Timer
for SDL in SDStr:
print( SDL)
t1 = Timer("Shudu(\"%s\").scanSDL()" % SDL, "from __main__ import Shudu")
print( sum(t1.repeat(10, 1))/10)
示例11: time_stmt
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def time_stmt(stmt='pass', setup='pass', number=0, repeat=3):
"""Timer function with the same behaviour as running `python -m timeit `
in the command line.
:return: elapsed time in seconds or NaN if the command failed.
:rtype: float
"""
t = Timer(stmt, setup)
if not number:
# determine number so that 0.2 <= total time < 2.0
for i in range(1, 10):
number = 10**i
try:
x = t.timeit(number)
except:
print(t.print_exc())
return float('NaN')
if x >= 0.2:
break
try:
r = t.repeat(repeat, number)
except:
print(t.print_exc())
return float('NaN')
best = min(r)
return best / number
示例12: speedtest
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def speedtest():
print "Adding %d keys without values" % len(data_add)
t = Timer("speed_insert(data_add,c_add)", "from __main__ import speed_insert,data_add,c_add")
print format_res(t.repeat(REPEAT, number=1))
print "Adding %d keys with values" % len(data_add_val)
t = Timer("speed_insert_val(data_add_val,c_val)", "from __main__ import speed_insert_val,data_add_val,c_val")
print format_res(t.repeat(REPEAT, number=1))
print "Getting %d keys" % len(data_get)
t = Timer("speed_get(data_get,c_val)", "from __main__ import speed_get,data_get,c_val")
print format_res(t.repeat(REPEAT, number=1))
print "Autocomp %d keys" % len(data_autocmp)
t = Timer("speed_autocmp(data_autocmp,c_val)", "from __main__ import speed_autocmp,data_autocmp,c_val")
print format_res(t.repeat(REPEAT, number=1))
示例13: benchmark
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def benchmark(stmt, setup="pass"):
timer = Timer(stmt, setup)
total = 0.0
k = 1
while total < 0.2:
total = min(timer.repeat(3, k))
k *= 10
return 10 * total / k
示例14: _check_cache_behaviour
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def _check_cache_behaviour(func):
from timeit import Timer
timer = Timer(func)
uncached_result = func()
uncached_time = min(timer.repeat(10, number=1))
with named_temporary_directory() as tmpdir, cache(tmpdir):
# Prime the cache
func()
cached_time = min(timer.repeat(10, number=1))
cached_result = func()
print "%s with cache: %s" % (func.__name__, cached_time)
print "%s without cache: %s" % (func.__name__, uncached_time)
return cached_time, uncached_time, cached_result, uncached_result
示例15: bench_code
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import repeat [as 别名]
def bench_code(setup, src, runs=3, number=10):
number_cl = 1
number_np = 1
timer_np = Timer(src+"; comm.Barrier()", setup)
t_np = min( timer_np.repeat(runs, number_np) ) / number_np
return t_np