Advertisement
kolypto

benchmark dict hashing

May 27th, 2019
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from time import time
  2. import json
  3. d = {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4],
  4.      'f': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  5.      'g': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  6.      'h': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  7.      'i': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  8.      'j': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4],
  9.      'f': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  10.      'g': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  11.      'h': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  12.      'i': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  13.      'j': {'a': 1, 'b': 2, 'c': 3, 'd': 'aaa', 'e': [1,2,3,4], 'f': {}},
  14.      }},
  15.      }
  16.  
  17. import copy
  18.  
  19. def make_hash(o):
  20.   """
  21.  Makes a hash from a dictionary, list, tuple or set to any level, that contains
  22.  only other hashable types (including any lists, tuples, sets, and
  23.  dictionaries).
  24.  """
  25.  
  26.   if isinstance(o, (set, tuple, list)):
  27.     return tuple([make_hash(e) for e in o])
  28.   elif not isinstance(o, dict):
  29.     return hash(o)
  30.  
  31.   new_o = copy.deepcopy(o)
  32.   for k, v in new_o.items():
  33.     new_o[k] = make_hash(v)
  34.  
  35.   return hash(tuple(frozenset(sorted(new_o.items()))))
  36.  
  37. N_REP = 10000
  38.  
  39. t = time()
  40. for i in range(N_REP):
  41.     hash(json.dumps(d, sort_keys=True))
  42. t = time() - t
  43. print(f"JSON: {t:.02f}s")
  44.  
  45.  
  46.  
  47. t = time()
  48. for i in range(N_REP):
  49.     make_hash(d)
  50. t = time() - t
  51. print(f"make_hash: {t:.02f}s")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement