-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
93 lines (78 loc) · 2.57 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import mmh3object
import mmh3
import timeit
# 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).
def mmh3object_purepython(o):
hash_func = mmh3.hash
t = type(o)
if t in (str, unicode, int, long, float, bool):
return hash_func(repr(o))
elif t in (tuple, list):
return hash_func(repr([mmh3object_purepython(e) for e in o]))
elif t is dict:
return hash_func(repr(sorted([(k, mmh3object_purepython(v)) for k, v in o.iteritems()])))
elif t in (set, frozenset):
return hash_func(repr(sorted([mmh3object_purepython(e) for e in o])))
elif isinstance(o, (tuple, list)):
return hash_func(repr([mmh3object_purepython(e) for e in o]))
elif isinstance(o, (set, frozenset)):
return hash_func(repr(sorted([mmh3object_purepython(e) for e in o])))
elif isinstance(o, dict):
return hash_func(repr(sorted([(k, mmh3object_purepython(v)) for k, v in o.iteritems()])))
else:
return hash_func(repr(o))
# Compatibility
for val in [
True,
None,
1,
0,
"heo",
u"heo",
{"a": 1},
{"a": {"b": set([3])}}
]:
# print val
# print mmh3object.mmh3object(val)
# print mmh3object_purepython(val)
assert mmh3object.mmh3object(val) == mmh3object_purepython(val)
N = 100000
# Performance
for val in [
{"a": {"b": set([3])}},
{
"price": {
"op": {
"t": {
"r": 0.2000000000000000111
},
"c": "EUR",
"p": 7.7500000000000008882
}
},
"hashes": {
"parsed_noprice": "izN3HPc+33AYigJxY+7FXw==",
"document": "EutG+msjMsx6XXXMsY6gDQ==",
"root": "OaIvv9gWFIgpLEfyTaz1VQ==",
"parsed": "BDxZPHX/9mR88zWIMQ+EJg==",
"full_parsed": "/DZ0/i8EY6l7FrQBShJsqA=="
},
"detected_page_type": "product",
"market": "fr_FR_EUR",
"refresh_interval": 24,
"fetch_flags": {
"page_type_error": False,
"delete_cp_error": False
},
"refetch": {
"lastfail": None,
"consecutivefails": 0,
},
"isparent": True
}
]:
print "Pure-python Performance for %s hashes of %s : %s" % (N, val, timeit.timeit('mmh3object_purepython(%s)' % (repr(val), ), number=N, setup="from __main__ import mmh3object_purepython"))
print "Cython Performance for %s hashes of %s : %s" % (N, val, timeit.timeit('mmh3object.mmh3object(%s)' % (repr(val), ), number=N, setup="from __main__ import mmh3object"))
print "Tests OK!"