import json import time from pprint import pprint from io import Bytes

 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
import json
import time
from pprint import pprint
from io import BytesIO
import ujson
import rapidjson
import ijson.backends.yajl2 as ijson
def build_node(level=1):
root = {}
if level <= 3:
for idx in range(5):
root['key_%d' % idx] = {
'level': level,
'id': '%s-%s' % (level, idx),
'subnodes': build_node(level + 1),
}
return root
str_node = json.dumps(build_node())
start = time.time()
for _ in range(1000):
rapidjson.loads(str_node)
print('rapidjson: %.2f' % (time.time() - start))
start = time.time()
for _ in range(1000):
ujson.loads(str_node)
print('ujson: %.2f' % (time.time() - start))
start = time.time()
for _ in range(1000):
for prefix, event, value in ijson.parse(BytesIO(str_node.encode())):
pass
print('ijson: %.2f' % (time.time() - start))