1+ import gc
12import os
23
34# set before importing `msgpack`
@@ -68,6 +69,9 @@ def serialize_other(obj, mapped):
6869 fd .write (packb (obj ))
6970
7071
72+ RAW_OBJ = stream (False )
73+ RAW_DATA = pack (RAW_OBJ )
74+
7175if __name__ == "__main__" :
7276 parser = argparse .ArgumentParser ()
7377 parser .add_argument ("-n" , "--number" , type = int , default = 25 , help = "Number of runs" )
@@ -77,40 +81,73 @@ def serialize_other(obj, mapped):
7781 _globals = {
7882 "main" : main ,
7983 "stream" : stream ,
84+ "raw" : unpack ,
8085 "other" : other ,
86+ "other_raw" : unpackb ,
87+ "data" : RAW_DATA ,
8188 "mapped" : args .mapped ,
8289 }
8390
8491 _serialize = {
8592 "main" : serialize_main ,
8693 "stream" : serialize_stream ,
94+ "raw" : pack ,
8795 "other" : serialize_other ,
88- "obj" : stream (False ),
96+ "other_raw" : packb ,
97+ "obj" : RAW_OBJ ,
8998 "mapped" : args .mapped ,
9099 }
91100
101+ gc .disable ()
102+
92103 t_main = timeit .timeit ("main(mapped)" , number = args .number , globals = _globals )
93104 t_stream = timeit .timeit ("stream(mapped)" , number = args .number , globals = _globals )
105+ t_raw = timeit .timeit ("raw(data)" , number = args .number , globals = _globals )
94106 t_other = timeit .timeit ("other(mapped)" , number = args .number , globals = _globals )
107+ t_other_raw = timeit .timeit (
108+ "other_raw(data, strict_map_key=False)" , number = args .number , globals = _globals
109+ )
110+
111+ gc .enable ()
112+ gc .collect ()
95113
96114 print (
97115 f"main: { t_main :.6f} s total, { t_main / args .number :.6f} s per call ({ t_other / t_main :.2f} x speedup vs msgpack)"
98116 )
99117 print (
100118 f"stream: { t_stream :.6f} s total, { t_stream / args .number :.6f} s per call ({ t_other / t_stream :.2f} x speedup vs msgpack)"
101119 )
120+ print (
121+ f"raw (unpack bytes): { t_raw :.6f} s total, { t_raw / args .number :.6f} s per call ({ t_other_raw / t_raw :.2f} x speedup vs msgpack raw)"
122+ )
102123 print (f"other (msgpack): { t_other :.6f} s total, { t_other / args .number :.6f} s per call" )
124+ print (
125+ f"other_raw (unpackb bytes): { t_other_raw :.6f} s total, { t_other_raw / args .number :.6f} s per call"
126+ )
127+
128+ gc .disable ()
103129
104130 t_main_s = timeit .timeit ("main(obj, mapped)" , number = args .number , globals = _serialize )
105131 t_stream_s = timeit .timeit ("stream(obj, mapped)" , number = args .number , globals = _serialize )
132+ t_raw_s = timeit .timeit ("raw(obj)" , number = args .number , globals = _serialize )
106133 t_other_s = timeit .timeit ("other(obj, mapped)" , number = args .number , globals = _serialize )
134+ t_other_raw_s = timeit .timeit ("other_raw(obj)" , number = args .number , globals = _serialize )
135+
136+ gc .enable ()
137+ gc .collect ()
107138
108139 print (
109140 f"main serialize: { t_main_s :.6f} s total, { t_main_s / args .number :.6f} s per call ({ t_other_s / t_main_s :.2f} x speedup vs msgpack)"
110141 )
111142 print (
112143 f"stream serialize: { t_stream_s :.6f} s total, { t_stream_s / args .number :.6f} s per call ({ t_other_s / t_stream_s :.2f} x speedup vs msgpack)"
113144 )
145+ print (
146+ f"raw serialize (pack bytes): { t_raw_s :.6f} s total, { t_raw_s / args .number :.6f} s per call ({ t_other_raw_s / t_raw_s :.2f} x speedup vs msgpack raw)"
147+ )
114148 print (
115149 f"other serialize (msgpack): { t_other_s :.6f} s total, { t_other_s / args .number :.6f} s per call"
116150 )
151+ print (
152+ f"other_raw serialize (packb bytes): { t_other_raw_s :.6f} s total, { t_other_raw_s / args .number :.6f} s per call"
153+ )
0 commit comments