Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- random.seed(109)
- from string import ascii_uppercase as alphabet
- zipf = [1/x for x in range(1, 1+26)]
- hay = [c for i, c in enumerate(alphabet)
- for _ in range(100_000//(i + 1))]
- random.shuffle(hay)
- hay = ''.join(hay)
- def zipf_string(length):
- start = random.randint(0, len(hay) - length)
- return hay[start:start+length]
- from itertools import product
- def do_timings():
- import pyperf
- runner = pyperf.Runner()
- # silence the warnings
- runner.parse_args()
- runner.args.quiet = True
- runner._process_priority = lambda *args: None
- ms = [2, 3, 4, 6, 8, 12, 16, 24, 32,
- 48, 64, 96, 128, 192, 256, 384, 512, 768,
- 1024, 1536, 2048, 3072, 4096, 6144]
- ns = [2000, 3000, 4000, 6000, 8000, 12000,
- 16000, 18000, 32000, 48000, 64000, 96000]
- for n, m in product(ns, ms):
- if m > n:
- continue
- random.seed(0)
- runner.timeit(f"needle={m}, haystack={n}",
- setup="needle = zipf_string(m); haystack = zipf_string(n)",
- stmt="needle in haystack",
- globals=globals() | locals())
- if __name__ == "__main__":
- do_timings()
Advertisement
Add Comment
Please, Sign In to add comment