-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_serde_benchmark.py
158 lines (121 loc) · 2.75 KB
/
simple_serde_benchmark.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import sys
import ujson
import msgpack
import pickle
import json
import msgspec
from timeit import default_timer as timer
def ujson_ser(o):
return ujson.dumps(o)
def ujson_de(b):
return ujson.loads(b)
def pickle_ser(o, protocol=pickle.DEFAULT_PROTOCOL):
return pickle.dumps(o, protocol=protocol)
def pickle_de(b):
return pickle.loads(b)
def msgpack_ser(o):
return msgpack.packb(o)
def msgpack_de(b):
return msgpack.unpackb(b, strict_map_key=False)
def json_ser(o):
return json.dumps(o)
def json_de(b):
return json.loads(b)
def msgspec_ser(o):
return msgspec.msgpack.encode(o)
def msgspec_de(b):
return msgspec.msgpack.decode(b)
N = 10_000
d_size = 1_000
d = {i: i for i in range(d_size)}
print(f'Obj size {N*(sys.getsizeof(d) / 1_000_000)} MB')
print("UJson measurements")
start = timer()
for _ in range(N):
B = ujson_ser(d)
end = timer()
t = round((end - start) * 1000, 4)
print(f"Ser time: {t} ms")
start = timer()
for _ in range(N):
ujson_de(B)
end = timer()
t2 = round((end - start) * 1000, 4)
t += t2
print(f"De time: {t2} ms")
print(f"Total: {t} ms\n")
print("Pickle DEFAULT measurements")
start = timer()
for _ in range(N):
B = pickle_ser(d)
end = timer()
t = round((end - start) * 1000, 4)
print(f"Ser time: {t} ms")
start = timer()
for _ in range(N):
pickle_de(B)
end = timer()
t2 = round((end - start) * 1000, 4)
t += t2
print(f"De time: {t2} ms")
print(f"Total: {t} ms\n")
print("Pickle HIGHEST measurements")
start = timer()
for _ in range(N):
B = pickle_ser(d, protocol=pickle.HIGHEST_PROTOCOL)
end = timer()
t = round((end - start) * 1000, 4)
print(f"Ser time: {t} ms")
start = timer()
for _ in range(N):
pickle_de(B)
end = timer()
t2 = round((end - start) * 1000, 4)
t += t2
print(f"De time: {t2} ms")
print(f"Total: {t} ms\n")
print("Msgpack measurements")
start = timer()
for _ in range(N):
B = msgpack_ser(d)
end = timer()
t = round((end - start) * 1000, 4)
print(f"Ser time: {t} ms")
start = timer()
for _ in range(N):
msgpack_de(B)
end = timer()
t2 = round((end - start) * 1000, 4)
t += t2
print(f"De time: {t2} ms")
print(f"Total: {t} ms\n")
print("Json measurements")
start = timer()
for _ in range(N):
B = json_ser(d)
end = timer()
t = round((end - start) * 1000, 4)
print(f"Ser time: {t} ms")
start = timer()
for _ in range(N):
json_de(B)
end = timer()
t2 = round((end - start) * 1000, 4)
t += t2
print(f"De time: {t2} ms")
print(f"Total: {t} ms\n")
print("Msgspec measurements")
start = timer()
for _ in range(N):
B = msgspec_ser(d)
end = timer()
t = round((end - start) * 1000, 4)
print(f"Ser time: {t} ms")
start = timer()
for _ in range(N):
msgspec_de(B)
end = timer()
t2 = round((end - start) * 1000, 4)
t += t2
print(f"De time: {t2} ms")
print(f"Total: {t} ms\n")