Last active
May 16, 2022 11:09
-
-
Save methane/9d2a35a519d89e657454f278e49fd4ae to your computer and use it in GitHub Desktop.
dict benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import pyperf | |
def build_dict_str(n): | |
mydict = {str(k): k for k in range(n)} | |
return mydict, list(mydict) | |
def build_dict_int(n): | |
mydict = {k: k for k in range(n)} | |
return mydict, list(mydict) | |
def bench_dict(loops, mydict, keys): | |
range_it = range(loops) | |
t0 = pyperf.perf_counter() | |
for loops in range_it: | |
for k in keys: | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
mydict[k] | |
return pyperf.perf_counter() - t0 | |
runner = pyperf.Runner() | |
# inner-loops: dict[str] is duplicated 10 times | |
runner.bench_time_func('dict[str, 10]', bench_dict, *build_dict_str(10), inner_loops=10*10) | |
runner.bench_time_func('dict[str, 1,000]', bench_dict, *build_dict_str(1000), inner_loops=10*1000) | |
runner.bench_time_func('dict[str, 1,000,000]', bench_dict, *build_dict_str(1000_000), inner_loops=10*1000_000) | |
runner.bench_time_func('dict[int, 10]', bench_dict, *build_dict_int(10), inner_loops=10*10) | |
runner.bench_time_func('dict[int, 1,000]', bench_dict, *build_dict_int(1000), inner_loops=10*1000) | |
runner.bench_time_func('dict[int, 1,000,000]', bench_dict, *build_dict_int(1000_000), inner_loops=10*1000_000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swisstable hash: methane/cpython#41