Created
September 18, 2014 22:33
-
-
Save charlax/b8731de51d2ea86c6eb9 to your computer and use it in GitHub Desktop.
json/ujson vs. make_hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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