首页 > 代码库 > FlatBuffers与protobuf性能比较
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//// DESC : basic message for msg-center//// AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014//// 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//// DESC : basic message for msg-center//// AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014//// 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 cachedMem: 66081944 65831100 250844 0 182240 46903452-/+ buffers/cache: 18745408 47336536Swap: 975864 724648 251216protobuf三次测试结果:./bin/tellist_test loop = 1000000, time diff = 14283msbuf size:841bin/tellist_test loop = 1000000, time diff = 14096msbuf size:841bin/tellist_test loop = 1000000, time diff = 14229msbuf size:841占用内存空间841Byte,平均运算时间42608ms / 3 = 14202.7msflatbuffers三次测试结果:bin/tellist_test loop = 1000000, time diff = 11694msbuf size:1712 bin/tellist_test loop = 1000000, time diff = 11710msbuf size:1712bin/tellist_test loop = 1000000, time diff = 11774msbuf size:1712占用内存空间1712Byte,平均运算时间35178ms / 3 = 11726ms测试环境:1 Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHzMEM total used free shared buffers cachedMem: 753932 432672 321260 0 89236 258052-/+ buffers/cache: 85384 668548Swap: 1324028 0 1324028protobuf三次测试结果:bin/tellist_test loop = 1000000, time diff = 12779msbuf size:841bin/tellist_test loop = 1000000, time diff = 13475msbuf size:841bin/tellist_test loop = 1000000, time diff = 12604msbuf size:841占用内存空间841Byte,平均运算时间38858ms / 3 = 12952.7msflatbuffers三次测试结果:bin/tellist_test loop = 1000000, time diff = 9424msbuf size:1712 bin/tellist_test loop = 1000000, time diff = 9277msbuf size:1712bin/tellist_test loop = 1000000, time diff = 9265msbuf size:1712info vecotor size:37, its right size:37占用内存空间1712Byte,平均运算时间28036ms / 3 = 9345ms
从以上数据看出,在内存空间占用这个指标上,FlatBuffers占用的内存空间比protobuf多了两倍,而二者的cpu计算时间虽然相差3000ms左右,但考虑到测试用了100万次,二者每次计算时间几乎没有差别。
从以上测试数据来看,FlatBuffers的性能并不如它吹嘘的那么好,个人稍有点失望。FB大量使用了C++11的语法,其从idl生成的代码接口也不如protubuf友好。不过相比使用protobuf时的一堆头文件和占18M之多的lib库,FlatBuffers仅仅一个"flatbuffers/flatbuffers.h"就足够了。
测试程序已经上传到百度网盘,点击这个链接即可下载。欢迎各位的批评意见。