Python timeit.timeit() Examples

The following are 30 code examples of timeit.timeit(). 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: benchmark.py    From python-tabulate with MIT License 6 votes vote down vote up
def benchmark(n):
    global methods
    if '--onlyself' in sys.argv[1:]:
        methods = [ m for m in methods if m[0].startswith("tabulate") ]
    else:
        methods = methods

    results = [(desc, timeit(code, setup_code, number=n)/n * 1e6)
               for desc, code in methods]
    mintime = min(map(lambda x: x[1], results))
    results = [(desc, t, t/mintime) for desc, t in
               sorted(results, key=lambda x: x[1])]
    table = tabulate.tabulate(results,
                              [u"Table formatter", u"time, μs", u"rel. time"],
                              u"rst", floatfmt=".1f")
    print codecs.encode(table, "utf-8") 
Example #2
Source File: test_common.py    From flask-security with MIT License 6 votes vote down vote up
def test_auth_token_speed(app, client_nc):
    # To run with old algorithm you have to comment out fs_uniquifier check in UserMixin
    import timeit

    response = json_authenticate(client_nc)
    token = response.json["response"]["user"]["authentication_token"]

    def time_get():
        rp = client_nc.get(
            "/login",
            data={},
            headers={"Content-Type": "application/json", "Authentication-Token": token},
        )
        assert rp.status_code == 200

    t = timeit.timeit(time_get, number=50)
    print("Time for 50 iterations: ", t) 
Example #3
Source File: ber.py    From asn1tools with MIT License 6 votes vote down vote up
def libsnmp_encode_decode():
    try:
        import libsnmp.rfc1905 as libsnmp_rfc1905

        def decode():
            libsnmp_rfc1905.Message().decode(ENCODED_MESSAGE)

        encode_time = float('inf')
        decode_time = timeit.timeit(decode, number=ITERATIONS)
    except ImportError:
        encode_time = float('inf')
        decode_time = float('inf')
        print('Unable to import libsnmp.')
    except SyntaxError:
        encode_time = float('inf')
        decode_time = float('inf')
        print('Syntax error in libsnmp.')

    return encode_time, decode_time 
Example #4
Source File: ber.py    From asn1tools with MIT License 6 votes vote down vote up
def pyasn1_encode_decode():
    try:
        from pysnmp.proto import api
        from pyasn1.codec.ber import decoder

        snmp_v1 = api.protoModules[api.protoVersion1].Message()

        def decode():
            decoder.decode(ENCODED_MESSAGE, asn1Spec=snmp_v1)

        encode_time = float('inf')
        decode_time = timeit.timeit(decode, number=ITERATIONS)
    except ImportError:
        encode_time = float('inf')
        decode_time = float('inf')
        print('Unable to import pyasn1.')

    return encode_time, decode_time 
Example #5
Source File: compile_methods.py    From asn1tools with MIT License 6 votes vote down vote up
def method_compile_file():
    print("Parsing and compiling '{}' from file... ".format(RRC_8_6_0_ASN_PATH),
          end='',
          flush=True)

    def compile_file():
        asn1tools.compile_files(RRC_8_6_0_ASN_PATH)

    time = timeit.timeit(compile_file, number=ITERATIONS)

    print('done.')

    with open(RRC_8_6_0_ASN_PATH, 'rb') as fin:
        string = fin.read()

    return round(time, 5), len(string) 
Example #6
Source File: opt_conv_cpu.py    From FlexTensor with MIT License 6 votes vote down vote up
def pytorch_baseliine(batch_size, height, width, channel, kernel_size, output_channel, stride, padding, number=100):
    # conv = torch.nn.Conv2d(channel, output_channel, (kernel_size, kernel_size), (stride, stride), (padding, padding), bias=False)
    # conv = torch.nn.functional.conv2d
    # A = torch.rand([batch_size, channel, height, width])
    # W = torch.rand([output_channel, channel, kernel_size, kernel_size])
    # # warm-up
    # conv(A, W)
    # beg = time.time()
    # for i in range(number):
    #     conv(A, W)
    # end = time.time()

    run_time = timeit.timeit(setup= 'import torch\n'
                                    'conv = torch.nn.functional.conv2d\n'
                                    'A = torch.rand([' + str(batch_size) + ', ' + str(channel) + ', ' + str(height) + ', ' + str(width) + '])\n'
                                    'W = torch.rand([' + str(output_channel) + ', ' + str(channel) + ', ' + str(kernel_size) + ', ' + str(kernel_size) + '])\n'
                                    'conv(A, W)\n',
                               stmt='conv(A, W)',
                               number=number)
    print("pytorch use {}ms".format(run_time / number * 1e3)) 
Example #7
Source File: compile_methods.py    From asn1tools with MIT License 6 votes vote down vote up
def method_compile_string():
    with open(RRC_8_6_0_ASN_PATH, 'r') as fin:
        string = fin.read()

    print("Parsing and compiling '{}'... ".format(RRC_8_6_0_ASN_PATH),
          end='',
          flush=True)

    def compile_string():
        asn1tools.compile_string(string)

    time = timeit.timeit(compile_string, number=ITERATIONS)

    print('done.')

    return round(time, 5), len(string) 
Example #8
Source File: scanprof.py    From aumfor with GNU General Public License v3.0 6 votes vote down vote up
def permscan(self, address_space, offset = 0, maxlen = None):
    times = []
    # Run a warm-up scan to ensure the file is cached as much as possible
    self.oldscan(address_space, offset, maxlen)

    perms = list(itertools.permutations(self.checks))
    for i in range(len(perms)):
        self.checks = perms[i]
        print "Running scan {0}/{1}...".format(i + 1, len(perms))
        profobj = ScanProfInstance(self.oldscan, address_space, offset, maxlen)
        value = timeit.timeit(profobj, number = self.repeats)
        times.append((value, len(list(profobj.results)), i))

    print "Scan results"
    print "{0:20} | {1:7} | {2:6} | {3}".format("Time", "Results", "Perm #", "Ordering")
    for val, l, ordering in sorted(times):
        print "{0:20} | {1:7} | {2:6} | {3}".format(val, l, ordering, perms[ordering])
    sys.exit(1) 
Example #9
Source File: test_fcs_reader.py    From fcsparser with MIT License 6 votes vote down vote up
def test_speed_of_reading_fcs_files(self):
        """Test the speed of loading a FCS files"""
        file_path = FILE_IDENTIFIER_TO_PATH['mq fcs 3.1']
        number = 1000

        time = timeit.timeit(
            lambda: parse_fcs(file_path, meta_data_only=True, reformat_meta=False), number=number)

        print('Loading fcs file {0} times with meta_data only without reformatting of '
              'meta takes {1} per loop'.format(time / number, number))

        time = timeit.timeit(
            lambda: parse_fcs(file_path, meta_data_only=True, reformat_meta=True), number=number)
        print('Loading fcs file {0} times with meta_data only with reformatting of '
              'meta takes {1} per loop'.format(time / number, number))

        time = timeit.timeit(
            lambda: parse_fcs(file_path, meta_data_only=False, reformat_meta=False), number=number)

        print('Loading fcs file {0} times both meta and data but without reformatting of '
              'meta takes {1} per loop'.format(time / number, number)) 
Example #10
Source File: test_array_op.py    From restrain-jit with MIT License 6 votes vote down vote up
def test_repeat_append_nojit():
    x = []
    for i in range(100000):
        x.append(i)
    return x


# print(test_repeat_append_jit())

# print(test_repeat_append_jit_foreach())
#
# for e in (test_repeat_append_jit_foreach.__func_info__.r_codeinfo.instrs):
#     print(e)
# show_instrs(test_repeat_append_jit_foreach.__func_info__.r_codeinfo.instrs)
#
# %timeit test_repeat_append_jit()
# %timeit test_repeat_append_nojit()
# %timeit test_repeat_append_jit_foreach() 
Example #11
Source File: bench_oneshot.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    print("%s methods involved on platform %r (%s iterations, psutil %s):" % (
        len(names), sys.platform, ITERATIONS, psutil.__version__))
    for name in sorted(names):
        print("    " + name)

    # "normal" run
    elapsed1 = timeit.timeit(
        "call_normal(funs)", setup=setup, number=ITERATIONS)
    print("normal:  %.3f secs" % elapsed1)

    # "one shot" run
    elapsed2 = timeit.timeit(
        "call_oneshot(funs)", setup=setup, number=ITERATIONS)
    print("onshot:  %.3f secs" % elapsed2)

    # done
    if elapsed2 < elapsed1:
        print("speedup: +%.2fx" % (elapsed1 / elapsed2))
    elif elapsed2 > elapsed1:
        print("slowdown: -%.2fx" % (elapsed2 / elapsed1))
    else:
        print("same speed") 
Example #12
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 #13
Source File: test_timeit.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def timeit(self, stmt, setup, number=None):
        self.fake_timer = FakeTimer()
        t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
        kwargs = {}
        if number is None:
            number = DEFAULT_NUMBER
        else:
            kwargs['number'] = number
        delta_time = t.timeit(**kwargs)
        self.assertEqual(self.fake_timer.setup_calls, 1)
        self.assertEqual(self.fake_timer.count, number)
        self.assertEqual(delta_time, number)

    # Takes too long to run in debug build.
    #def test_timeit_default_iters(self):
    #    self.timeit(self.fake_stmt, self.fake_setup) 
Example #14
Source File: templates_parse_perf.py    From influxgraph with Apache License 2.0 5 votes vote down vote up
def test_python_template(self):
        setup = '\n'.join(["from influxgraph.templates import get_series_with_tags",
                           self.timeit_setup])
        parse_time = timeit(
            stmt=self.template_stmt, setup=setup, number=100)
        pprint("Python template parse time is %s" % (parse_time,)) 
Example #15
Source File: bilinear_baseline.py    From FlexTensor with MIT License 5 votes vote down vote up
def pytorch_cpu(N, K1, K2, M, number=100, dev=0):
    run_time = timeit.timeit(setup= 'import torch\n'
                                    'A = torch.rand([' + str(N) + ', ' + str(K1) + '], dtype=torch.float32)\n'
                                    'B = torch.rand([' + str(N) + ', ' + str(K2) + '], dtype=torch.float32)\n'
                                    'C = torch.rand([' + str(M) + ', ' + str(K1) + ', ' + str(K2) + '], dtype=torch.float32)\n'
                                    'torch.nn.functional.bilinear(A, B, C)\n',
                               stmt='ans = torch.nn.functional.bilinear(A, B, C)',
                               number=number)
    return run_time / number * 1e3 
Example #16
Source File: conv_transpose1d_baseline.py    From FlexTensor with MIT License 5 votes vote down vote up
def pytorch_cpu(batch_size, length, channel, kernel_size, output_channel, stride=1, padding=0, dilation=1, groups=1, number=100, dev=0):
    run_time = timeit.timeit(setup= 'import torch\n'
                                    'conv = torch.nn.functional.conv_transpose1d\n'
                                    'A = torch.rand([' + str(batch_size) + ', ' + str(channel) + ', ' + str(length) + '], dtype=torch.float32)\n'
                                    'W = torch.rand([' + str(channel) + ', ' + str(output_channel//groups) + ', ' + str(kernel_size) + '], dtype=torch.float32)\n'
                                    'conv(A, W, stride=' + str(stride) + ', padding=' + str(padding) + ', dilation=' + str(dilation) + ', groups=' + str(groups) + ')\n',
                               stmt='ans = conv(A, W, stride=' + str(stride) + ', padding=' + str(padding) + ', dilation=' + str(dilation) + ', groups=' + str(groups) + ')',
                               number=number)
    return run_time / number * 1e3 
Example #17
Source File: grouped_baseline.py    From FlexTensor with MIT License 5 votes vote down vote up
def pytorch_cpu(batch_size, height, width, channel, kernel_size, output_channel, stride=1, padding=0, dilation=1, groups=1, number=100, dev=0):
    run_time = timeit.timeit(setup= 'import torch\n'
                                    'conv = torch.nn.functional.conv2d\n'
                                    'A = torch.rand([' + str(batch_size) + ', ' + str(channel) + ', ' + str(height) + ', ' + str(width) + '], dtype=torch.float32)\n'
                                    'W = torch.rand([' + str(output_channel) + ', ' + str(channel//groups) + ', ' + str(kernel_size) + ', ' + str(kernel_size) + '], dtype=torch.float32)\n'
                                    'conv(A, W, stride=' + str(stride) + ', padding=' + str(padding) + ', dilation=' + str(dilation) + ', groups=' + str(groups) + ')\n',
                               stmt='ans = conv(A, W, stride=' + str(stride) + ', padding=' + str(padding) + ', dilation=' + str(dilation) + ', groups=' + str(groups) + ')',
                               number=number)
    return run_time / number * 1e3 
Example #18
Source File: test_utils.py    From oggm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_clips_performance(self):
        import timeit

        n = int(1e5)

        # Scalar
        s1 = 'import numpy as np'
        s2 = 'from oggm import utils'

        t1 = timeit.timeit('np.clip(1, 0, 10)', number=n, setup=s1)
        t2 = timeit.timeit('utils.clip_scalar(1, 0, 10)', number=n, setup=s2)
        assert t2 < t1

        t1 = timeit.timeit('np.clip(12, None, 10)', number=n, setup=s1)
        t2 = timeit.timeit('utils.clip_max(12, 10)', number=n, setup=s2)
        assert t2 < t1

        t1 = timeit.timeit('np.clip(12, 15, None)', number=n, setup=s1)
        t2 = timeit.timeit('utils.clip_min(12, 15)', number=n, setup=s2)
        assert t2 < t1

        # Array
        s1 = 'import numpy as np; a = np.arange(50) - 5'
        s2 = 'from oggm import utils; import numpy as np; a = np.arange(50)-5'

        t1 = timeit.timeit('np.clip(a, 0, 10)', number=n, setup=s1)
        t2 = timeit.timeit('utils.clip_array(a, 0, 10)', number=n, setup=s2)
        # This usually fails as advertised by numpy
        # (although with np 1.17 not)
        # assert t2 < t1

        t1 = timeit.timeit('np.clip(a, None, 10)', number=n, setup=s1)
        t2 = timeit.timeit('utils.clip_max(a, 10)', number=n, setup=s2)
        assert t2 < t1

        t1 = timeit.timeit('np.clip(a, 15, None)', number=n, setup=s1)
        t2 = timeit.timeit('utils.clip_min(a, 15)', number=n, setup=s2)
        assert t2 < t1 
Example #19
Source File: index_perf.py    From influxgraph with Apache License 2.0 5 votes vote down vote up
def time_index(self, index_import, insert_stmt, tmpl_stmt):
        return timeit(
            stmt=insert_stmt,
            setup=self.timeit_setup % (self.series, index_import), number=10), \
            timeit(
                stmt=tmpl_stmt,
                setup="\n".join([self.timeit_setup % (self.templates, index_import)]),
                number=10), \
            timeit(stmt=self.timeit_query_stmt,
                   setup="\n".join([self.timeit_setup % (self.series, index_import),
                                    self.py_timeit_insert_stmt]),
                   number=10) 
Example #20
Source File: templates_parse_perf.py    From influxgraph with Apache License 2.0 5 votes vote down vote up
def test_cython_template(self):
        setup = '\n'.join(["""
try:
    from influxgraph.ext.templates import get_series_with_tags
except ImportError:
    from influxgraph.templates import get_series_with_tags
""",
                           self.timeit_setup])
        parse_time = timeit(
            stmt=self.template_stmt, setup=setup, number=100)
        pprint("Cython template parse time is %s" % (parse_time,)) 
Example #21
Source File: gemm_baseline.py    From FlexTensor with MIT License 5 votes vote down vote up
def pytorch_cpu(N, K, M, number=100, dev=0):
    run_time = timeit.timeit(setup= 'import torch\n'
                                    'A = torch.rand([' + str(N) + ', ' + str(K) + '], dtype=torch.float32)\n'
                                    'B = torch.rand([' + str(K) + ', ' + str(M) + '], dtype=torch.float32)\n'
                                    'torch.mm(A, B)\n',
                               stmt='ans = torch.mm(A, B)',
                               number=number)
    return run_time / number * 1e3 
Example #22
Source File: conv_transpose3d_baseline.py    From FlexTensor with MIT License 5 votes vote down vote up
def pytorch_cpu(batch_size, depth, height, width, channel, kernel_size, output_channel, stride=1, padding=0, dilation=1, groups=1, number=100, dev=0):
    run_time = timeit.timeit(setup= 'import torch\n'
                                    'conv = torch.nn.functional.conv_transpose3d\n'
                                    'A = torch.rand([' + str(batch_size) + ', ' + str(channel) + ', ' + str(depth) + ', ' + str(height) + ', ' + str(width) + '], dtype=torch.float32)\n'
                                    'W = torch.rand([' + str(channel) + ', ' + str(output_channel//groups) + ', ' + str(kernel_size) + ', ' + str(kernel_size) + ', ' + str(kernel_size) + '], dtype=torch.float32)\n'
                                    'conv(A, W, stride=' + str(stride) + ', padding=' + str(padding) + ', dilation=' + str(dilation) + ', groups=' + str(groups) + ')\n',
                               stmt='ans = conv(A, W, stride=' + str(stride) + ', padding=' + str(padding) + ', dilation=' + str(dilation) + ', groups=' + str(groups) + ')',
                               number=number)
    return run_time / number * 1e3 
Example #23
Source File: test_lru_cache.py    From loopchain with Apache License 2.0 5 votes vote down vote up
def test_lru_cache(self):
        # GIVEN
        test_number = 100
        func_without_cache = partial(self.get_big_list_without_cache, test_number)
        func_with_cache = partial(self.get_big_list_with_cache, test_number)

        # WHEN
        without_cache = timeit.timeit(func_without_cache, number=test_number)
        with_cache = timeit.timeit(func_with_cache, number=test_number)
        util.logger.debug(f"timeit with cache({with_cache}), without cache({without_cache})")

        # THEN
        self.assertGreater(without_cache, with_cache) 
Example #24
Source File: grid_client.py    From PySyft with Apache License 2.0 5 votes vote down vote up
def _get_download_speed(self, worker_id, random_id):
        params = {"worker_id": worker_id, "random": random_id}
        speed_history = []
        with requests.get(self.http_url + "/federated/speed-test", params, stream=True) as r:
            r.raise_for_status()
            buffer_size = CHUNK_SIZE
            chunk_generator = self._yield_chunk_from_request(r, CHUNK_SIZE)
            for _ in range(MAX_SPEED_TESTS):
                time_taken = timeit(
                    lambda: self._read_n_request_chunks(chunk_generator, buffer_size // CHUNK_SIZE),
                    number=1,
                )
                if time_taken < 0.5:
                    buffer_size = min(buffer_size * SPEED_MULT_FACTOR, MAX_BUFFER_SIZE)
                    continue
                new_speed = buffer_size / (time_taken * 1024)
                speed_history.append(new_speed)
                if len(speed_history) % CHECK_SPEED_EVERY == 0:
                    avg = sum(speed_history) / len(speed_history)
                    deviation = avg - min(speed_history)
                    if (deviation < 20) and (avg > 0):
                        break

        if len(speed_history) == 0:
            return -1
        else:
            avg_speed = sum(speed_history) / len(speed_history)
            return avg_speed 
Example #25
Source File: grid_client.py    From PySyft with Apache License 2.0 5 votes vote down vote up
def _get_upload_speed(self, worker_id, random_id):
        buffer_size = CHUNK_SIZE
        speed_history = []

        for _ in range(MAX_SPEED_TESTS):
            data_sample = b"x" * buffer_size
            params = {"worker_id": worker_id, "random": random_id}
            body = {"upload_data": data_sample}
            time_taken = timeit(
                lambda: self._send_http_req("POST", "/federated/speed-test", params, body),
                number=1,
            )
            if time_taken < 0.5:
                buffer_size = min(
                    buffer_size * SPEED_MULT_FACTOR, MAX_BUFFER_SIZE * 64
                )  # 64 MB max file size
                continue
            upload_speed = 64 * 1024 / time_taken  # speed in KBps
            speed_history.append(upload_speed)
            if len(speed_history) % CHECK_SPEED_EVERY == 0:
                avg = sum(speed_history) / len(speed_history)
                deviation = avg - min(speed_history)
                if (deviation < 20) and (avg > 0):
                    break
        if len(speed_history) == 0:
            return -1
        else:
            avg_speed = sum(speed_history) / len(speed_history)
            return avg_speed 
Example #26
Source File: grid_client.py    From PySyft with Apache License 2.0 5 votes vote down vote up
def _get_ping(self, worker_id, random_id):
        params = {"is_ping": 1, "worker_id": worker_id, "random": random_id}
        ping = (
            timeit(
                lambda: self._send_http_req("GET", "/federated/speed-test", params),
                number=MAX_SPEED_TESTS,
            )
            * 1000
        )  # for ms
        return ping 
Example #27
Source File: benchmark.py    From textdistance with MIT License 5 votes vote down vote up
def get_internal_benchmark():
        for alg in libraries.get_algorithms():
            yield Lib(
                algorithm=alg,
                library='**textdistance**',
                function=alg,
                time=timeit(
                    stmt=STMT,
                    setup=INTERNAL_SETUP.format(alg),
                    number=RUNS,
                ),
                presets=None,
            ) 
Example #28
Source File: benchmark.py    From textdistance with MIT License 5 votes vote down vote up
def get_external_benchmark(installed):
        for lib in installed:
            yield lib._replace(time=timeit(
                stmt=STMT,
                setup=EXTERNAL_SETUP.format(**lib._asdict()),
                number=RUNS,
            )) 
Example #29
Source File: bing_speech_api.py    From respeaker_python_library with Apache License 2.0 5 votes vote down vote up
def main():
    import timeit
    import logging

    logging.basicConfig(level=logging.DEBUG)

    bing = BingSpeechAPI()

    def test(text, stream=None):
        try:
            print('TTS:{}'.format(text))
            speech = bing.synthesize(text, stream=stream)
            text = bing.recognize(speech, language='en-US')
            print('STT:{}'.format(text.encode('utf-8')))
            print('Stream mode:{}'.format('yes' if stream else 'no'))
        except RequestError as e:
            print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))

    texts = [
        'Your beliefs become your thoughts',
        'Your thoughts become your words',
        'Your words become your actions',
        'Your actions become your habits',
        'Your habits become your values',
        'Your values become your destiny',
    ]

    for n, text in enumerate(texts):
        print('No.{} try'.format(n))
        print(timeit.timeit(lambda: test(text, n & 1), number=1)) 
Example #30
Source File: test_hashing.py    From flask-security with MIT License 5 votes vote down vote up
def test_argon2_speed(app, sqlalchemy_datastore):
    init_app_with_options(
        app,
        sqlalchemy_datastore,
        **{
            "SECURITY_PASSWORD_HASH": "argon2",
            "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {"argon2__rounds": 10},
        }
    )
    with app.app_context():
        print(
            "Hash time for {} iterations: {}".format(
                100, timeit.timeit(lambda: hash_password("pass"), number=100)
            )
        )