使用Protocol Buffers更快地反序列化的建议

问题描述 投票:0回答:1

我使用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 Bit OS中平均需要大约96 ms。仅供参考,我还打印了msg.size(),发现它约为3773550。

我正在寻找比这更快地反序列化数据的建议。

c++ serialization protocol-buffers
1个回答
1
投票

简短的回答,可能没有办法。

Protobuf反序列化很慢,因为它需要从一系列键值对中动态构造对象。如果你担心性能问题,可以试试flatbuffer或Capn'Proto。这些是不需要任何对象构造的替代方案,但(可能)在磁盘上花费更多,并且还有其他缺点。

© www.soinside.com 2019 - 2024. All rights reserved.