FlatBuffers与protobuf性能比较

FlatBuffers发布时,顺便也公布了它的性能数据,具体数据请见 Benchmark 。

它的测试用例由以下数据构成" a set of about 10 objects containing an array, 4 strings, and a large variety of int/float scalar values of all sizes, meant to be representative of game data, e.g. a scene format."

   我感觉这样测试如同儿戏,便自己设计了一个测试用例,主要关注CPU计算时间和内存空间占用两个指标,参考对象是protobuf。

   测试用例为:序列化一个通讯录personal_info_list(table),通讯录可以认为是有每个人的信息(personal_info)的集合。每个人信息personal_info(table)有:个人id(uint)、名字(string)、年龄(byte)、性别(enum, byte)和电话号码(ulong)。本来我想用struct表示personal_info(table),但是struct不允许有数组或string成员,无奈我用table描述它了。相应的idl文件如下:

 
  //// FILE : tellist.fbs
 
 
  //// LICENCE :
  //// MOD :
 
 
  namespace   as.tellist;
 
  enum   GENDER_TYPE : byte
  {
  MALE = 0,
  FEMALE = 1,
  OTHER = 2
  }
 
  table personal_info
  {
  id : uint;
  name : string;
  age : byte;
  gender : GENDER_TYPE;
  phone_num : ulong;
  }
 
  table personal_info_list
  {
  info : [personal_info];
  }
 
  root_type personal_info_list;

   因为要以protobuf做性能参考,列出protobuf的idl文件如下:    

 
  //// FILE : tellist.proto
 
 
  //// LICENCE :
  //// MOD :
 
 
  package as.tellist;
 
  enum   gender_type
  {
  MALE = 0;
  FEMALE = 1;
  OTHER = 2;
  }
 
  message personal_info
  {
  optional uint32 id = 1;
  optional string name = 2;
  optional uint32 age = 3;
  optional gender_type gender = 4;
  optional uint64 phone_num = 5;
  }
 
  message personal_info_list
  {
  repeated personal_info info = 1;
  }

在内存中构造37个personal_info对象,并序列化之,重复这个过程100万次。

    测试结果如下:

  测试环境:12Core Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
  free
  total used free shared buffers cached
  Mem: 66081944 65831100 250844 0 182240 46903452
  -/+ buffers/cache: 18745408 47336536
  Swap: 975864 724648 251216
 
  protobuf三次测试结果:
  ./bin/tellist_test
  loop = 1000000, time diff = 14283ms
  buf size:841
 
  bin/tellist_test
  loop = 1000000, time diff = 14096ms
  buf size:841
 
  bin/tellist_test
  loop = 1000000, time diff = 14229ms
  buf size:841
  占用内存空间841Byte,平均运算时间42608ms / 3 = 14202.7ms
 
  flatbuffers三次测试结果:
  bin/tellist_test
  loop = 1000000, time diff = 11694ms
  buf size:1712
 
  bin/tellist_test
  loop = 1000000, time diff = 11710ms
  buf size:1712
 
  bin/tellist_test
  loop = 1000000, time diff = 11774ms
  buf size:1712
  占用内存空间1712Byte,平均运算时间35178ms / 3 = 11726ms
 
 
  测试环境:1 Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
  MEM
  total used free shared buffers cached
  Mem: 753932 432672 321260 0 89236 258052
  -/+ buffers/cache: 85384 668548
  Swap: 1324028 0 1324028
 
  protobuf三次测试结果:
  bin/tellist_test
  loop = 1000000, time diff = 12779ms
  buf size:841
 
  bin/tellist_test
  loop = 1000000, time diff = 13475ms
  buf size:841
 
  bin/tellist_test
  loop = 1000000, time diff = 12604ms
  buf size:841
  占用内存空间841Byte,平均运算时间38858ms / 3 = 12952.7ms
 
  flatbuffers三次测试结果:
  bin/tellist_test
  loop = 1000000, time diff = 9424ms
  buf size:1712
 
  bin/tellist_test
  loop = 1000000, time diff = 9277ms
  buf size:1712
 
  bin/tellist_test
  loop = 1000000, time diff = 9265ms
  buf size:1712
  info vecotor size:37, its right size:37
  占用内存空间1712Byte,平均运算时间28036ms / 3 = 9345ms

从以上数据看出,在内存空间占用这个指标上,FlatBuffers占用的内存空间比protobuf多了两倍,而二者的cpu计算时间虽然相差3000ms左右,但考虑到测试用了100万次,二者每次计算时间几乎没有差别。

从以上测试数据来看,FlatBuffers的性能并不如它吹嘘的那么好。个人稍有点失望。

你可能感兴趣的:(FlatBuffers与protobuf性能比较)