- Python版本: 3.6.1
- Protocol Buffer版本: 3.5.2post1
- MsgPack版本: 0.5.6
Protocol Buffer与MsgPack均为C扩展版
proto文件:
syntax = "proto3";
message TestPacket {
int32 a = 1;
int64 b = 2;
float c = 3;
string d = 4;
uint64 e = 5;
string f = 6;
int32 g = 7;
}
python文件:
import msgpack
import uuid
import time
from protobuf.client import test_pb2 as t_packet
def start_proto_test(count):
start_time = time.time()
test_packet = t_packet.TestPacket()
test_packet.a = 99999
test_packet.b = 888888
test_packet.c = 999999.99
test_packet.d = '111111111111111111111111111'
test_packet.e = uuid.uuid1().int >> 64
test_packet.f = '222222222222222222222222222'
test_packet.g = 1234567
out_data = None
for _ in range(0, count):
out_data = test_packet.SerializeToString()
use_time = (time.time() - start_time)
print('Serialize Protocol Buffer use: {}'.format(use_time))
return out_data
def start_decode_proto_test(count, data):
start_time = time.time()
for _ in range(0, count):
out_data = t_packet.TestPacket()
out_data.ParseFromString(data)
use_time = (time.time() - start_time)
print('Parse Protocol Buffer use: {}'.format(use_time))
def start_msgpack_test(count):
start_time = time.time()
test_packet = dict()
test_packet['a'] = 99999
test_packet['b'] = 888888
test_packet['c'] = 999999.99
test_packet['d'] = '111111111111111111111111111'
test_packet['e'] = uuid.uuid1().int >> 64
test_packet['f'] = '222222222222222222222222222'
test_packet['g'] = 1234567
out_data = None
for _ in range(0, count):
out_data = msgpack.packb(test_packet, use_bin_type=True)
use_time = (time.time() - start_time)
print('Serialize MsgPack use: {}'.format(use_time))
return out_data
def start_decode_msgpack_test(count, data):
start_time = time.time()
for _ in range(0, count):
msgpack.unpackb(data, raw=False)
use_time = (time.time() - start_time)
print('Parse MsgPack use: {}'.format(use_time))
pack_count = 1000000
print('Test count: {}'.format(pack_count))
print('-----------------------')
proto_data = start_proto_test(pack_count)
start_decode_proto_test(pack_count, proto_data)
print('Protocol Buffer Pack Size: {}'.format(len(proto_data)))
print('-----------------------')
msg_data = start_msgpack_test(pack_count)
start_decode_msgpack_test(pack_count, msg_data)
print('MsgPack Pack Size: {}'.format(len(msg_data)))结果:
Test count: 1000000
-----------------------
Serialize Protocol Buffer use: 2.514462947845459
Parse Protocol Buffer use: 2.593277931213379
Protocol Buffer Pack Size: 86
-----------------------
Serialize MsgPack use: 10.78027081489563
Parse MsgPack use: 1.2247998714447021
MsgPack Pack Size: 102同一包结构,Protocol Buffer序列化速度是MsgPack的4倍左右,而MsgPack得反序列化速度是Protocol Buffer的2倍左右。Protocol Buffer序列化后的包比MsgPack小 1/6 左右。
版权声明:本文为blackman954原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。