Python timeit.repeat() Examples

The following are 30 code examples of timeit.repeat(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module timeit , or try the search function .
Example #1
Source File: test_timeit.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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) 
Example #2
Source File: test_timeit.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: test_timeit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #4
Source File: test_util.py    From asttokens with Apache License 2.0 6 votes vote down vote up
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)))'))) 
Example #5
Source File: test_mark_tokens.py    From asttokens with Apache License 2.0 6 votes vote down vote up
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))) 
Example #6
Source File: continous_local_opt_benchmarks.py    From bingo with Apache License 2.0 6 votes vote down vote up
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() 
Example #7
Source File: evaluation_benchmark.py    From bingo with Apache License 2.0 6 votes vote down vote up
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 
Example #8
Source File: test_timeit.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #9
Source File: Matchup_test.py    From incubator-sdap-nexus with Apache License 2.0 6 votes vote down vote up
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))) 
Example #10
Source File: fitness_benchmark.py    From bingo with Apache License 2.0 6 votes vote down vote up
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) 
Example #11
Source File: cu2qu.py    From cu2qu with Apache License 2.0 6 votes vote down vote up
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)) 
Example #12
Source File: benchmark.py    From cu2qu with Apache License 2.0 6 votes vote down vote up
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.)) 
Example #13
Source File: NumericUtil.py    From refinery with MIT License 6 votes vote down vote up
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
########################################################### 
Example #14
Source File: listcomp_speed.py    From example-code with MIT License 5 votes vote down vote up
def clock(label, cmd):
    res = timeit.repeat(cmd, setup=SETUP, number=TIMES)
    print(label, *('{:.3f}'.format(x) for x in res)) 
Example #15
Source File: parallel_archipelago_benchmarks.py    From bingo with Apache License 2.0 5 votes vote down vote up
def do_benchmarking():
    printer = IslandStatsPrinter()
    printer.add_stats("Explicit Regression",
                      timeit.repeat(explicit_regression_benchmark,
                                    number=2,
                                    repeat=2))
    printer.print() 
Example #16
Source File: container_perftest.py    From example-code with MIT License 5 votes vote down vote up
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))) 
Example #17
Source File: __init__.py    From funcfinder with MIT License 5 votes vote down vote up
def _time(question, answer, number=1):
    def stmt():
        question(answer)

    if number == 1:
        time_taken = 0
        while time_taken < 1:
            number *= 2
            time_taken = timeit.timeit(stmt, number=number)
    return min(timeit.repeat(stmt, number=number, repeat=5)), number 
Example #18
Source File: test_lines.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_invisible_Line_rendering():
    """
    Github issue #1256 identified a bug in Line.draw method

    Despite visibility attribute set to False, the draw method was not
    returning early enough and some pre-rendering code was executed
    though not necessary.

    Consequence was an excessive draw time for invisible Line instances
    holding a large number of points (Npts> 10**6)
    """
    # Creates big x and y data:
    N = 10**7
    x = np.linspace(0, 1, N)
    y = np.random.normal(size=N)

    # Create a plot figure:
    fig = plt.figure()
    ax = plt.subplot(111)

    # Create a "big" Line instance:
    l = mlines.Line2D(x, y)
    l.set_visible(False)
    # but don't add it to the Axis instance `ax`

    # [here Interactive panning and zooming is pretty responsive]
    # Time the canvas drawing:
    t_no_line = min(timeit.repeat(fig.canvas.draw, number=1, repeat=3))
    # (gives about 25 ms)

    # Add the big invisible Line:
    ax.add_line(l)

    # [Now interactive panning and zooming is very slow]
    # Time the canvas drawing:
    t_unvisible_line = min(timeit.repeat(fig.canvas.draw, number=1, repeat=3))
    # gives about 290 ms for N = 10**7 pts

    slowdown_factor = (t_unvisible_line/t_no_line)
    slowdown_threshold = 2  # trying to avoid false positive failures
    assert slowdown_factor < slowdown_threshold 
Example #19
Source File: island_benchmarks.py    From bingo with Apache License 2.0 5 votes vote down vote up
def do_benchmarking():
    printer = IslandStatsPrinter()
    printer.add_stats("Explicit Regression",
                      timeit.repeat(explicit_regression_benchmark,
                                    number=4,
                                    repeat=4))
    printer.print() 
Example #20
Source File: test_timeit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_repeat_few_reps_and_iters(self):
        self.repeat(self.fake_stmt, self.fake_setup, repeat=3, number=5) 
Example #21
Source File: test_timeit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_repeat_zero_iters(self):
        self.repeat(self.fake_stmt, self.fake_setup, number=0) 
Example #22
Source File: patcher.py    From volatility with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #23
Source File: test_timeit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_repeat_zero_reps(self):
        self.repeat(self.fake_stmt, self.fake_setup, repeat=0) 
Example #24
Source File: profile_bk.py    From IntraArchiveDeduplicator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def timeit_py():
	pt.buildTestTree()
	ret = timeit.repeat("pt.random_q()", "from __main__ import pt; import random; random.seed()", number=2000)
	print("timing results: ", ret) 
Example #25
Source File: patcher.py    From vortessence with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #26
Source File: patcher.py    From DAMM with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #27
Source File: profile_bk.py    From IntraArchiveDeduplicator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def timeit_c():
	pt.buildCppTree()
	ret = timeit.repeat("pt.random_q()", "from __main__ import pt; import random; random.seed()", number=2000)
	print("timing results: ", ret) 
Example #28
Source File: tree_benchmark.py    From tree with Apache License 2.0 5 votes vote down vote up
def run_benchmark(benchmark_fn, num_iters):
  times = timeit.repeat(benchmark_fn, repeat=2, number=num_iters)
  return times[-1] / num_iters  # Discard the first half for "warmup". 
Example #29
Source File: NumericUtil.py    From refinery with MIT License 5 votes vote down vote up
def runTimingExperiment_inplaceExpAndNormalizeRows(N=2e5, D=100, repeat=3):
  if not hasNumexpr:
    return 0
  setup = "import numpy as np; import numexpr as ne;"
  setup += "from bnpy.util import NumericUtil as N;"
  setup += "R = np.random.rand(%d, %d)" % (N, D)
  elapsedTimes_np = timeit.repeat("N.inplaceExpAndNormalizeRows_numpy(R)", 
                    setup=setup, number=1, repeat=repeat)
  elapsedTimes_ne = timeit.repeat("N.inplaceExpAndNormalizeRows_numexpr(R)",
                    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 
Example #30
Source File: NumericUtil.py    From refinery with MIT License 5 votes vote down vote up
def runTimingExperiment_calcRlogR(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)
  elapsedTimes_np = timeit.repeat("N.calcRlogR_numpy(R)", 
                    setup=setup, number=1, repeat=repeat)
  elapsedTimes_ne = timeit.repeat("N.calcRlogR_numexpr(R)",
                    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