forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydataclass
executable file
·131 lines (109 loc) · 3.34 KB
/
pydataclass
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import dataclasses
import io
import json
import os
from timeit import timeit
from typing import List
import orjson
import rapidjson
import simplejson
from tabulate import tabulate
os.sched_setaffinity(os.getpid(), {0, 1})
buf = io.StringIO()
@dataclasses.dataclass
class Member:
id: int
active: bool
@dataclasses.dataclass
class Object:
id: int
name: str
members: List[Member]
objects_as_dataclass = [
Object(i, str(i) * 3, [Member(j, True) for j in range(0, 10)])
for i in range(100000, 101000)
]
objects_as_dict = [dataclasses.asdict(each) for each in objects_as_dataclass]
def default(__obj):
if dataclasses.is_dataclass(__obj):
return dataclasses.asdict(__obj)
headers = ("Library", "dict (ms)", "dataclass (ms)", "dataclass vs. dict", "vs. orjson")
LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json")
ITERATIONS = 100
table = []
for lib_name in LIBRARIES:
if lib_name == "json":
as_dict = timeit(
lambda: json.dumps(objects_as_dict, default=default).encode("utf-8"),
number=ITERATIONS,
)
as_dataclass = timeit(
lambda: json.dumps(objects_as_dataclass, default=default).encode("utf-8"),
number=ITERATIONS,
)
elif lib_name == "simplejson":
as_dict = timeit(
lambda: simplejson.dumps(objects_as_dict, default=default).encode("utf-8"),
number=ITERATIONS,
)
as_dataclass = timeit(
lambda: simplejson.dumps(objects_as_dataclass, default=default).encode(
"utf-8"
),
number=ITERATIONS,
)
elif lib_name == "ujson":
as_dict = None
as_dataclass = None
elif lib_name == "rapidjson":
as_dict = timeit(
lambda: rapidjson.dumps(objects_as_dict, default=default).encode("utf-8"),
number=ITERATIONS,
)
as_dataclass = timeit(
lambda: rapidjson.dumps(objects_as_dataclass, default=default).encode(
"utf-8"
),
number=ITERATIONS,
)
elif lib_name == "orjson":
as_dict = timeit(lambda: orjson.dumps(objects_as_dict), number=ITERATIONS)
as_dataclass = timeit(
lambda: orjson.dumps(
objects_as_dataclass, None, orjson.OPT_SERIALIZE_DATACLASS
),
number=ITERATIONS,
)
orjson_as_dataclass = as_dataclass
else:
raise NotImplementedError
relative = (
int(((as_dataclass - as_dict) / as_dataclass) * 100) if as_dataclass else None
)
if lib_name == "orjson":
compared_to_orjson = 1
elif as_dict:
compared_to_orjson = int(as_dataclass / orjson_as_dataclass)
else:
compared_to_orjson = None
table.append(
(
lib_name,
f"{as_dict:,.2f}" if as_dict else "",
f"{as_dataclass:,.2f}" if as_dataclass else "",
f"*{relative}%" if relative else "",
f"{compared_to_orjson:d}" if compared_to_orjson else "",
)
)
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")
print(
buf.getvalue()
.replace("-", "")
.replace("*", "-")
.replace("=", "-")
.replace("+", "|")
.replace("||||||", "")
.replace("\n\n", "\n")
)