我正在使用 Protocol Buffers为了序列化/反序列化数据。我定义了 Protocol Buffers 消息文件如下:
syntax = "proto3";
package Tutorial;
import "google/protobuf/timestamp.proto";
message PointCloud {
int32 width = 1;
int32 height = 2;
message Point {
float x = 1;
float y = 2;
float z = 3;
fixed32 rgb = 4;
}
repeated Point points = 3;
google.protobuf.Timestamp timestamp = 4;
}
我能够接收序列化数据。我正在使用 ParseFromArray API如下:
zmq::message_t msg;
int rc = zmq_socket.recv(&msg);
if (rc){
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
Tutorial::PointCloud point_cloud;
point_cloud.ParseFromArray(msg.data(), msg.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time (ms): " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()/1000.0 << std::endl
}
上面的方法有效,但反序列化数据需要足够的时间。在 Ubuntu 14.04 LTS 64 位操作系统中平均需要大约 96 毫秒。仅供引用,我还打印了 msg.size()
并发现它大约为 3773550。
我正在寻找比这更快的反序列化数据的建议。
最佳答案
简答,可能没有办法。
Protobuf 的反序列化速度很慢,因为它需要从一系列键值对中动态构建对象。如果您关心性能,请尝试 flatbuffer 或 Capn'Proto。这些替代方案不需要任何对象构造,但(可能)在磁盘上花费更多,并且有其他缺点。
关于c++ - 使用 Protocol Buffers 更快反序列化的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902201/