我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用timeit.repeat()。
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.))
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)))')))
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)))
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)
def timer(fn): """ Create a timeit call to a function and pass in keyword arguments. The function is called twice, once using the standard workbook, then with the optimised one. Time from the best of three is taken. """ print("lxml", openpyxl.LXML) result = [] for opt in (False, True,): print("Workbook is {0}".format(opt and "optimised" or "not optimised")) times = timeit.repeat("{0}({1})".format(fn.__name__, opt), setup="from __main__ import {0}".format(fn.__name__), number = 1, repeat = 3 ) print("{0:.2f}s".format(min(times))) result.append(min(times)) std, opt = result print("Optimised takes {0:.2%} time\n".format(opt/std)) return std, opt
def test_speed_np_builtins(size, nLoop, nRep=1): setupCode = ( "import numpy as np;" + "PRNG = np.random.RandomState(0);" + "x = PRNG.rand(%d);" % (size) ) pprint_timeit( stmt='np.argmax(x)', setup=setupCode, number=nLoop, repeat=nRep) pprint_timeit( stmt='np.argsort(x)', setup=setupCode, number=nLoop, repeat=nRep) nnzPerRows = [0] for expval in np.arange(0, np.ceil(np.log2(size / 2))): nnzPerRows.append(2**expval) for nnzPerRow in nnzPerRows: funcCode = 'np.argpartition(x, %d)' % (nnzPerRow) pprint_timeit( stmt=funcCode, setup=setupCode, number=nLoop, repeat=nRep)
def check(self): timeme = self.item_options['timeme'] requests = timeme['requests'] if 'requests' in timeme else 5 limit_max = timeme.get('limit_max') limit_avg = timeme.get('limit_avg') from requests import Session s = Session() request = URLUtils.prepare_request(self.url, self.global_options, self.item_options) times = timeit.repeat(stmt=lambda:s.send(request, timeout=30, allow_redirects=True), repeat=requests, number=1) request_max = max(times) request_avg = reduce(lambda x, y: x + y, times) / len(times) if limit_max and request_max > limit_max: self.fail("Maximum request time greater than limit: {0} > {1}".format(request_max, limit_max)) if limit_avg and request_avg > limit_avg: self.fail("Average request time greater than limit: {0} > {1}".format(request_avg, limit_avg)) return self.is_ok()
def clock(label, cmd): res = timeit.repeat(cmd, setup=SETUP, number=TIMES) print(label, *('{:.3f}'.format(x) for x in res))
def test(container_type, verbose): MAX_EXPONENT = 7 for n in range(3, MAX_EXPONENT + 1): size = 10**n setup = SETUP.format(container_type=container_type, size=size, verbose=verbose) test = TEST.format(verbose=verbose) tt = timeit.repeat(stmt=test, setup=setup, repeat=5, number=1) print('|{:{}d}|{:f}'.format(size, MAX_EXPONENT + 1, min(tt)))
def do_tests(): for test_key in test_keys: func_name = 'exists_and_truthy_' + test_key test = func_name + '(gizmo, "gadget")' setup = 'from __main__ import gizmo, ' + func_name elapsed = average(timeit.repeat(test, repeat=5, setup=setup)) print(test_key.rjust(7), format(elapsed, '0.5f'))
def test(): for test_key in test_keys: test_name = 'test_' + test_key test = globals()[test_name] setup = 'from __main__ import gizmo' t_present = min(timeit.repeat(test, setup=setup)) del gizmo.gadget t_absent = min(timeit.repeat(test, setup=setup)) gizmo.gadget = True print('{:7} {:.3f} {:.3f}'.format(test_key, t_present, t_absent))
def run_timeit(quotes, iterations, repeat, jit=False, load=False, cython=False, profile=False): quotes_schema = QuoteSchema(many=True) if jit: if cython: quotes_schema.jit = CythonJit else: quotes_schema.jit = Jit if profile: profile = cProfile.Profile() profile.enable() dumped_quotes = quotes_schema.dump(quotes).data gc.collect() if load: def marshmallow_func(): quotes_schema.load(dumped_quotes, many=True) else: def marshmallow_func(): quotes_schema.dump(quotes) best = min(timeit.repeat(marshmallow_func, 'gc.enable()', number=iterations, repeat=repeat)) if profile: profile.disable() file_name = 'optimized.pprof' if jit else 'original.pprof' profile.dump_stats(file_name) usec = best * 1e6 / iterations return usec
def time(stmt, iterations, setup): print('Timing: ' + stmt) times = timeit.repeat(stmt=stmt, number=iterations, setup=setup) best = min(times) print('{0} loops, best of 3: {1}'.format(iterations, best)) return best
def bench(name, func): print('{}: {:.3f} seconds'.format(name, min(timeit.repeat(func, number=1, repeat=3))))
def test_repeat_zero_reps(self): self.repeat(self.fake_stmt, self.fake_setup, repeat=0)
def test_repeat_zero_iters(self): self.repeat(self.fake_stmt, self.fake_setup, number=0)
def test_repeat_few_reps_and_iters(self): self.repeat(self.fake_stmt, self.fake_setup, repeat=3, number=5)
def test_repeat_callable_stmt(self): self.repeat(self.fake_callable_stmt, self.fake_setup, repeat=3, number=5)
def test_repeat_function_zero_reps(self): delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, repeat=0, timer=FakeTimer()) self.assertEqual(delta_times, [])
def test_repeat_function_zero_iters(self): delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, number=0, timer=FakeTimer()) self.assertEqual(delta_times, DEFAULT_REPEAT * [0.0])
def use_fullpage(self, address_space): """Calibrate the scanner to ensure fastest speed""" # Define the calibration functions timeit_fullpage = lambda: list(self.scan_page(address_space, 0, True)) timeit_nonfullpage = lambda: list(self.scan_page(address_space, 0, False)) with_fullpage = timeit.repeat(timeit_fullpage, number = 100) without_fullpage = timeit.repeat(timeit_nonfullpage, number = 100) return min(with_fullpage) < min(without_fullpage)
def test_repeat_callable_stmt_and_setup(self): self.repeat(self.fake_callable_stmt, self.fake_callable_setup, repeat=3, number=5) # Takes too long to run in debug build. #def test_repeat_function(self): # delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, # timer=FakeTimer()) # self.assertEqual(delta_times, DEFAULT_REPEAT * [float(DEFAULT_NUMBER)])
def profile(name, func): times = timeit.repeat(func, number=1000, repeat=4) times = ', '.join(["%8f" % t for t in times]) print("%-30s %40s" % (name, times))