Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. import re
  2. import functools
  3.  
  4.  
  5. def do_re(s, rdict):
  6.  
  7.     r = re.compile("|".join(rdict.keys()))
  8.     return r.sub(lambda m: rdict[m.group(0)], s)
  9.  
  10.  
  11. def do_str(s, rdict):
  12.  
  13.     return functools.reduce(lambda s, k: s.replace(k, rdict[k]), rdict, s)
  14.  
  15.  
  16. if __name__ == "__main__":
  17.  
  18.     import timeit
  19.     import itertools
  20.  
  21.     words = tuple(map("".join, itertools.product("abcdefg", repeat=5)))
  22.     s = str.join(" ", words)
  23.     rdict = {word: "yoba" for word in words}
  24.     print(len(s))
  25.     print(timeit.timeit(lambda: do_re(s, rdict), number=10))
  26.     print(timeit.timeit(lambda: do_str(s, rdict), number=10))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement