Skip to content

Commit c305fb5

Browse files
committed
Fix timeit latency
1 parent 058f7b9 commit c305fb5

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ dataclasses, those with optional or default attributes, and subclasses.
263263

264264
| Library | dict (ms) | dataclass (ms) | vs. orjson |
265265
|------------|-------------|------------------|--------------|
266-
| orjson | 0.10 | 0.19 | 1 |
266+
| orjson | 1.80 | 2.87 | 1 |
267267
| ujson | | | |
268-
| rapidjson | 0.24 | 6.48 | 33 |
269-
| simplejson | 1.06 | 7.94 | 40 |
270-
| json | 0.92 | 7.32 | 37 |
268+
| rapidjson | 4.23 | 91.79 | 31 |
269+
| simplejson | 19.26 | 113.70 | 39 |
270+
| json | 14.37 | 107.38 | 37 |
271271

272-
This measures orjson serializing instances natively and other libraries using
273-
`default` to serialize the output of `dataclasses.asdict()`. This can be
272+
This measures serializing 555KiB of JSON, orjson natively and other libraries
273+
using `default` to serialize the output of `dataclasses.asdict()`. This can be
274274
reproduced using the `pydataclass` script.
275275

276276
Dataclasses are serialized as maps, with every attribute serialized and in

pydataclass

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ from tabulate import tabulate
1717
os.sched_setaffinity(os.getpid(), {0, 1})
1818

1919

20-
buf = io.StringIO()
21-
22-
2320
@dataclasses.dataclass
2421
class Member:
2522
id: int
@@ -35,11 +32,15 @@ class Object:
3532

3633
objects_as_dataclass = [
3734
Object(i, str(i) * 3, [Member(j, True) for j in range(0, 10)])
38-
for i in range(100000, 101000)
35+
for i in range(100000, 102000)
3936
]
4037

4138
objects_as_dict = [dataclasses.asdict(each) for each in objects_as_dataclass]
4239

40+
output_in_kib = len(orjson.dumps(objects_as_dict)) / 1024
41+
42+
print(f"{output_in_kib:,.0f}KiB output (orjson)")
43+
4344

4445
def default(__obj):
4546
if dataclasses.is_dataclass(__obj):
@@ -52,6 +53,13 @@ LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json")
5253

5354
ITERATIONS = 100
5455

56+
57+
def per_iter_latency(val):
58+
if val is None:
59+
return None
60+
return (val * 1000) / ITERATIONS
61+
62+
5563
table = []
5664
for lib_name in LIBRARIES:
5765
if lib_name == "json":
@@ -96,10 +104,13 @@ for lib_name in LIBRARIES:
96104
),
97105
number=ITERATIONS,
98106
)
99-
orjson_as_dataclass = as_dataclass
107+
orjson_as_dataclass = per_iter_latency(as_dataclass)
100108
else:
101109
raise NotImplementedError
102110

111+
as_dict = per_iter_latency(as_dict)
112+
as_dataclass = per_iter_latency(as_dataclass)
113+
103114
if lib_name == "orjson":
104115
compared_to_orjson = 1
105116
elif as_dict:
@@ -115,6 +126,8 @@ for lib_name in LIBRARIES:
115126
f"{compared_to_orjson:d}" if compared_to_orjson else "",
116127
)
117128
)
129+
130+
buf = io.StringIO()
118131
buf.write(tabulate(table, headers, tablefmt="grid") + "\n")
119132

120133
print(

0 commit comments

Comments
 (0)