33# set before importing `msgpack`
44os .environ ["MSGPACK_PUREPYTHON" ] = "1"
55
6- from msgpack_stream import unpack , unpack_stream
7- from mmap import mmap , ACCESS_READ
6+ from msgpack_stream import unpack , unpack_stream , pack , pack_stream
7+ from mmap import mmap , ACCESS_READ , ACCESS_WRITE
88import argparse
99import timeit
1010
11- from msgpack import unpackb
11+ from msgpack import unpackb , packb
1212
1313
1414FILE = "scripts/obj.msgpack"
1515
1616
1717def main (mapped ):
18- with open (FILE , "rb" , buffering = 0 ) as fd :
18+ with open (FILE , "rb" ) as fd :
1919 if mapped :
2020 with mmap (fd .fileno (), 0 , access = ACCESS_READ ) as mm :
2121 return unpack (mm .read ())
@@ -24,7 +24,7 @@ def main(mapped):
2424
2525
2626def stream (mapped ):
27- with open (FILE , "rb" , buffering = 0 ) as fd :
27+ with open (FILE , "rb" ) as fd :
2828 if mapped :
2929 with mmap (fd .fileno (), 0 , access = ACCESS_READ ) as mm :
3030 return unpack_stream (mm )
@@ -33,29 +33,88 @@ def stream(mapped):
3333
3434
3535def other (mapped ):
36- with open (FILE , "rb" , buffering = 0 ) as fd :
36+ with open (FILE , "rb" ) as fd :
3737 if mapped :
3838 with mmap (fd .fileno (), 0 , access = ACCESS_READ ) as mm :
3939 return unpackb (mm .read (), strict_map_key = False )
4040 else :
4141 return unpackb (fd .read (), strict_map_key = False )
4242
4343
44+ def serialize_main (obj , mapped ):
45+ with open ("temp" , "wb" ) as fd :
46+ if mapped :
47+ with mmap (- 1 , 3955122 , access = ACCESS_WRITE ) as mm :
48+ mm .write (pack (obj ))
49+ else :
50+ fd .write (pack (obj ))
51+
52+
53+ def serialize_stream (obj , mapped ):
54+ with open ("temp" , "wb" ) as fd :
55+ if mapped :
56+ with mmap (- 1 , 3955122 , access = ACCESS_WRITE ) as mm :
57+ pack_stream (mm , obj )
58+ else :
59+ pack_stream (fd , obj )
60+
61+
62+ def serialize_other (obj , mapped ):
63+ with open ("temp" , "wb" ) as fd :
64+ if mapped :
65+ with mmap (- 1 , 3955122 , access = ACCESS_WRITE ) as mm :
66+ mm .write (packb (obj ))
67+ else :
68+ fd .write (packb (obj ))
69+
70+
4471if __name__ == "__main__" :
72+ parser = argparse .ArgumentParser ()
73+ parser .add_argument ("-n" , "--number" , type = int , default = 25 , help = "Number of runs" )
74+ parser .add_argument (
75+ "-m" , "--mapped" , action = "store_true" , help = "Use memory mapping"
76+ )
77+ args = parser .parse_args ()
78+
4579 _globals = {
4680 "main" : main ,
4781 "stream" : stream ,
4882 "other" : other ,
83+ "mapped" : args .mapped ,
84+ }
85+
86+ _serialize = {
87+ "main" : serialize_main ,
88+ "stream" : serialize_stream ,
89+ "other" : serialize_other ,
90+ "obj" : stream (False ),
91+ "mapped" : args .mapped ,
4992 }
50- parser = argparse .ArgumentParser ()
51- parser .add_argument ("-n" , "--number" , type = int , default = 25 , help = "Number of runs" )
52- args = parser .parse_args ()
5393
54- t_main = timeit .timeit ("main(True)" , number = args .number , globals = _globals )
55- # this needs to be mmap for good performance
56- t_stream = timeit .timeit ("stream(True)" , number = args .number , globals = _globals )
57- t_other = timeit .timeit ("other(True)" , number = args .number , globals = _globals )
94+ t_main = timeit .timeit ("main(mapped)" , number = args .number , globals = _globals )
95+ t_stream = timeit .timeit ("stream(mapped)" , number = args .number , globals = _globals )
96+ t_other = timeit .timeit ("other(mapped)" , number = args .number , globals = _globals )
5897
5998 print (f"main: { t_main :.6f} s total, { t_main / args .number :.6f} s per call" )
6099 print (f"stream: { t_stream :.6f} s total, { t_stream / args .number :.6f} s per call" )
61100 print (f"other: { t_other :.6f} s total, { t_other / args .number :.6f} s per call" )
101+
102+ t_main_s = timeit .timeit (
103+ "main(obj, mapped)" , number = args .number , globals = _serialize
104+ )
105+ t_stream_s = timeit .timeit (
106+ "stream(obj, mapped)" , number = args .number , globals = _serialize
107+ )
108+ t_other_s = timeit .timeit (
109+ "other(obj, mapped)" , number = args .number , globals = _serialize
110+ )
111+
112+ print (
113+ f"main serialize: { t_main_s :.6f} s total, { t_main_s / args .number :.6f} s per call"
114+ )
115+ print (
116+ f"stream serialize: { t_stream_s :.6f} s total, { t_stream_s / args .number :.6f} s per call"
117+ )
118+ print (
119+ f"other serialize: { t_other_s :.6f} s total, { t_other_s / args .number :.6f} s per call"
120+ )
0 commit comments