@@ -17,9 +17,6 @@ from tabulate import tabulate
1717os .sched_setaffinity (os .getpid (), {0 , 1 })
1818
1919
20- buf = io .StringIO ()
21-
22-
2320@dataclasses .dataclass
2421class Member :
2522 id : int
@@ -35,11 +32,15 @@ class Object:
3532
3633objects_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
4138objects_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
4445def default (__obj ):
4546 if dataclasses .is_dataclass (__obj ):
@@ -52,6 +53,13 @@ LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json")
5253
5354ITERATIONS = 100
5455
56+
57+ def per_iter_latency (val ):
58+ if val is None :
59+ return None
60+ return (val * 1000 ) / ITERATIONS
61+
62+
5563table = []
5664for 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 ()
118131buf .write (tabulate (table , headers , tablefmt = "grid" ) + "\n " )
119132
120133print (
0 commit comments