本文整理汇总了Python中timeit.repeat方法的典型用法代码示例。如果您正苦于以下问题:Python timeit.repeat方法的具体用法?Python timeit.repeat怎么用?Python timeit.repeat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类timeit
的用法示例。
在下文中一共展示了timeit.repeat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: repeat
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def repeat(self, stmt, setup, repeat=None, number=None):
self.fake_timer = FakeTimer()
t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
kwargs = {}
if repeat is None:
repeat = DEFAULT_REPEAT
else:
kwargs['repeat'] = repeat
if number is None:
number = DEFAULT_NUMBER
else:
kwargs['number'] = number
delta_times = t.repeat(**kwargs)
self.assertEqual(self.fake_timer.setup_calls, repeat)
self.assertEqual(self.fake_timer.count, repeat * number)
self.assertEqual(delta_times, repeat * [float(number)])
# Takes too long to run in debug build.
#def test_repeat_default(self):
# self.repeat(self.fake_stmt, self.fake_setup)
示例2: print_timing
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def print_timing(self):
# pylint: disable=no-self-use
# Test the implementation of asttokens.util.walk, which uses the same approach as
# visit_tree(). This doesn't run as a normal unittest, but if you'd like to see timings, e.g.
# after experimenting with the implementation, run this to see them:
#
# nosetests -i print_timing -s tests.test_util
#
import timeit
import textwrap
setup = textwrap.dedent(
'''
import ast, asttokens
source = "foo(bar(1 + 2), 'hello' + ', ' + 'world')"
atok = asttokens.ASTTokens(source, parse=True)
''')
print("ast", sorted(timeit.repeat(
setup=setup, number=10000,
stmt='len(list(ast.walk(atok.tree)))')))
print("util", sorted(timeit.repeat(
setup=setup, number=10000,
stmt='len(list(asttokens.util.walk(atok.tree)))')))
示例3: print_timing
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def print_timing(self):
# Print the timing of mark_tokens(). This doesn't normally run as a unittest, but if you'd like
# to see timings, e.g. while optimizing the implementation, run this to see them:
#
# nosetests -m print_timing -s tests.test_mark_tokens tests.test_astroid
#
# pylint: disable=no-self-use
import timeit
print("mark_tokens", sorted(timeit.repeat(
setup=textwrap.dedent(
'''
import ast, asttokens
source = "foo(bar(1 + 2), 'hello' + ', ' + 'world')"
atok = asttokens.ASTTokens(source)
tree = ast.parse(source)
'''),
stmt='atok.mark_tokens(tree)',
repeat=3,
number=1000)))
示例4: runTimingExperiment_calcRlogRdotv
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def runTimingExperiment_calcRlogRdotv(N=2e5, D=100, repeat=3):
if not hasNumexpr:
return 0
setup = "import numpy as np; import numexpr as ne;"
setup += "import bnpy.util.NumericUtil as N;"
setup += "R = np.random.rand(%d, %d);" % (N, D)
setup += "v = np.random.rand(%d)" % (N)
elapsedTimes_np = timeit.repeat("N.calcRlogRdotv_numpy(R, v)",
setup=setup, number=1, repeat=repeat)
elapsedTimes_ne = timeit.repeat("N.calcRlogRdotv_numexpr(R, v)",
setup=setup, number=1, repeat=repeat)
meanTime_np = np.mean(elapsedTimes_np)
meanTime_ne = np.mean(elapsedTimes_ne)
expectedGainFactor = meanTime_np / meanTime_ne
return expectedGainFactor
########################################################### MAIN
###########################################################
示例5: test_time_many_primary_many_matchup
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def test_time_many_primary_many_matchup(self):
import logging
import sys
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt="%Y-%m-%dT%H:%M:%S", stream=sys.stdout)
log = logging.getLogger(__name__)
# Generate 160000 DomsPoints distributed equally in a box from -2.0 lat/lon to 2.0 lat/lon
log.info("Generating primary points")
x = np.arange(-2.0, 2.0, 0.01)
y = np.arange(-2.0, 2.0, 0.01)
primary_points = [DomsPoint(longitude=xy[0], latitude=xy[1], time=1000, depth=5.0, data_id=i) for i, xy in
enumerate(np.array(np.meshgrid(x, y)).T.reshape(-1, 2))]
# Generate 2000 DomsPoints distributed randomly in a box from -2.0 lat/lon to 2.0 lat/lon
log.info("Generating matchup points")
matchup_points = [
DomsPoint(longitude=random.uniform(-2.0, 2.0), latitude=random.uniform(-2.0, 2.0), time=1000, depth=5.0,
data_id=i) for i in xrange(0, 2000)]
log.info("Starting matchup")
log.info("Best of repeat(3, 2) matchups: %s seconds" % min(
timeit.repeat(lambda: list(match_points_generator(primary_points, matchup_points, 1500)), repeat=3,
number=2)))
示例6: _add_stats_and_log_intermediate_steps
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def _add_stats_and_log_intermediate_steps(stats_printer,
regression,
run_name,
setup_function):
if DEBUG:
print("Running", regression.__name__, "...................")
stats_printer.add_stats(
run_name,
timeit.repeat(regression, setup=setup_function, number=1,
repeat=CLO_TIMING_REPEATS),
number=CLO_TIMING_NUMBER * NUM_AGRAPHS_INDVS,
unit_mult=1000
)
if DEBUG:
print(regression.__name__, "finished\n")
_reset_iteration_count()
示例7: do_benchmarking
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def do_benchmarking():
printer = benchmark_data.StatsPrinter("EVALUATION BENCHMARKS")
for backend, name in [[pyBackend, "py"], [cppBackend, "c++"]]:
agraph_module.Backend = backend
printer.add_stats(name + ": evaluate",
timeit.repeat(benchmark_evaluate,
number=EVAL_TIMING_NUMBER,
repeat=EVAL_TIMING_REPEATS),
number=EVAL_TIMING_NUMBER * NUM_AGRAPHS_INDVS,
unit_mult=1000)
printer.add_stats(name + ": x derivative",
timeit.repeat(benchmark_evaluate_w_x_derivative,
number=EVAL_TIMING_NUMBER,
repeat=EVAL_TIMING_REPEATS),
number=EVAL_TIMING_NUMBER * NUM_AGRAPHS_INDVS,
unit_mult=1000)
printer.add_stats(name + ": c derivative",
timeit.repeat(benchmark_evaluate_w_c_derivative,
number=EVAL_TIMING_NUMBER,
repeat=EVAL_TIMING_REPEATS),
number=EVAL_TIMING_NUMBER * NUM_AGRAPHS_INDVS,
unit_mult=1000)
return printer
示例8: _run_benchmarks
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def _run_benchmarks(printer, regression, regression_cpp):
for backend, name in [[pyBackend, " py"], [cppBackend, "c++"]]:
agraph_module.Backend = backend
printer.add_stats(
"py: fitness " + name + ": evaluate ",
timeit.repeat(regression,
number=FITNESS_TIMING_NUMBER,
repeat=FITNESS_TIMING_REPEATS),
number=FITNESS_TIMING_NUMBER * NUM_AGRAPHS_INDVS,
unit_mult=1000)
printer.add_stats(
"c++: fitness c++: evaluate ",
timeit.repeat(regression_cpp,
number=FITNESS_TIMING_NUMBER,
repeat=FITNESS_TIMING_REPEATS),
number=FITNESS_TIMING_NUMBER * NUM_AGRAPHS_INDVS,
unit_mult=1000)
示例9: run_benchmark
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def run_benchmark(
benchmark_module, module, function, setup_suffix='', repeat=5, number=1000):
setup_func = 'setup_' + function
if setup_suffix:
print('%s with %s:' % (function, setup_suffix), end='')
setup_func += '_' + setup_suffix
else:
print('%s:' % function, end='')
def wrapper(function, setup_func):
function = globals()[function]
setup_func = globals()[setup_func]
def wrapped():
return function(*setup_func())
return wrapped
results = timeit.repeat(wrapper(function, setup_func), repeat=repeat, number=number)
print('\t%5.1fus' % (min(results) * 1000000. / number))
示例10: run_benchmark
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def run_benchmark(
benchmark_module, module, function, setup_suffix='', repeat=1000):
setup_func = 'setup_' + function
if setup_suffix:
print('%s with %s:' % (function, setup_suffix), end='')
setup_func += '_' + setup_suffix
else:
print('%s:' % function, end='')
results = timeit.repeat(
'%s(*args)' % function,
setup=(SETUP_CODE % {
'benchmark_module': benchmark_module, 'setup_function': setup_func,
'module': module, 'function': function}),
repeat=repeat, number=1)
print('\tavg=%dus' % (sum(results) / len(results) * 1000000.),
'\tmin=%dus' % (min(results) * 1000000.))
示例11: timeit
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def timeit(stmt, globals=globals()):
import numpy as np
import timeit as _timeit
print("Timing '{0}'".format(stmt))
# Rough approximation of a 10 runs
trial = _timeit.timeit(stmt, globals=globals, number=10)/10
# Maximum duration
duration = 5.0
# Number of repeat
repeat = 7
# Compute rounded number of trials
number = max(1,int(10**np.ceil(np.log((duration/repeat)/trial)/np.log(10))))
# Only report best run
times = _timeit.repeat(stmt, globals=globals, number=number, repeat=repeat)
times = np.array(times)/number
mean = np.mean(times)
std = np.std(times)
# Display results
units = {"s": 1, "ms": 1e-3, "us": 1e-6, "ns": 1e-9}
for key,value in units.items():
unit, factor = key, 1/value
if mean > value: break
mean *= factor
std *= factor
print("%.3g %s ± %.3g %s per loop (mean ± std. dev. of %d runs, %d loops each)" %
(mean, unit, std, unit, repeat, number))
示例12: test_repeat_zero_reps
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def test_repeat_zero_reps(self):
self.repeat(self.fake_stmt, self.fake_setup, repeat=0)
示例13: test_repeat_zero_iters
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def test_repeat_zero_iters(self):
self.repeat(self.fake_stmt, self.fake_setup, number=0)
示例14: test_repeat_few_reps_and_iters
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def test_repeat_few_reps_and_iters(self):
self.repeat(self.fake_stmt, self.fake_setup, repeat=3, number=5)
示例15: test_repeat_callable_stmt
# 需要导入模块: import timeit [as 别名]
# 或者: from timeit import repeat [as 别名]
def test_repeat_callable_stmt(self):
self.repeat(self.fake_callable_stmt, self.fake_setup,
repeat=3, number=5)