sweeneyde

Untitled

Jan 9th, 2021
495
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. random.seed(109)
  3. from string import ascii_uppercase as alphabet
  4. zipf = [1/x for x in range(1, 1+26)]
  5.  
  6. hay = [c for i, c in enumerate(alphabet)
  7.          for _ in range(100_000//(i + 1))]
  8. random.shuffle(hay)
  9. hay = ''.join(hay)
  10.  
  11. def zipf_string(length):
  12.     start = random.randint(0, len(hay) - length)
  13.     return hay[start:start+length]
  14.  
  15. from itertools import product
  16.  
  17. def do_timings():
  18.     import pyperf
  19.     runner = pyperf.Runner()
  20.  
  21.     # silence the warnings
  22.     runner.parse_args()
  23.     runner.args.quiet = True
  24.     runner._process_priority = lambda *args: None
  25.  
  26.     ms = [2,       3,    4,    6,    8,   12,  16,  24,  32,
  27.           48,     64,   96,  128,  192,  256, 384, 512, 768,
  28.           1024, 1536, 2048, 3072, 4096, 6144]
  29.     ns = [2000,   3000,  4000,  6000,  8000, 12000,
  30.           16000, 18000, 32000, 48000, 64000, 96000]
  31.  
  32.     for n, m in product(ns, ms):
  33.         if m > n:
  34.             continue
  35.  
  36.         random.seed(0)
  37.         runner.timeit(f"needle={m}, haystack={n}",
  38.                       setup="needle = zipf_string(m); haystack = zipf_string(n)",
  39.                       stmt="needle in haystack",
  40.                       globals=globals() | locals())
  41.  
  42. if __name__ == "__main__":
  43.     do_timings()
  44.  
RAW Paste Data