Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
json/ujson vs. make_hash
In [1]: import ujson
In [2]: d = {"e": {"e": {"e": {c: str(c) for c in range(100)}}}}
In [3]: %paste
import copy
def make_hash(o):
"""
Makes a hash from a dictionary, list, tuple or set to any level, that contains
only other hashable types (including any lists, tuples, sets, and
dictionaries).
"""
if isinstance(o, (set, tuple, list)):
return tuple([make_hash(e) for e in o])
elif not isinstance(o, dict):
return hash(o)
new_o = copy.deepcopy(o)
for k, v in new_o.items():
new_o[k] = make_hash(v)
return hash(tuple(frozenset(sorted(new_o.items()))))
## -- End pasted text --
In [4]: %timeit hash(ujson.dumps(d))
100000 loops, best of 3: 19.3 µs per loop
In [5]: %timeit make_hash(d)
1000 loops, best of 3: 1.72 ms per loop
In [6]: d = {"e": {c: str(c) for c in range(100)}
...: }
In [7]: %timeit hash(ujson.dumps(d))
100000 loops, best of 3: 17.6 µs per loop
In [8]: %timeit make_hash(d)
1000 loops, best of 3: 915 µs per loop
In [9]: d = {c: str(c) for c in range(100)}
In [10]: %timeit hash(ujson.dumps(d))
100000 loops, best of 3: 15.8 µs per loop
In [11]: %timeit make_hash(d)
1000 loops, best of 3: 529 µs per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment