首页 > 代码库 > 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文件如下:

01//////////////////////////////////////////////////////
02//// FILE     : tellist.fbs
03//// DESC     : basic message for msg-center
04//// AUTHOR   : v 0.1 written by Alex Stocks on June 22, 2014
05//// LICENCE  :
06//// MOD      :
07////////////////////////////////////////////////////////
08 
09namespace as.tellist;
10 
11enum GENDER_TYPE : byte
12{
13    MALE    = 0,
14    FEMALE  = 1,
15    OTHER   = 2
16}
17 
18table personal_info
19{
20    id : uint;
21    name : string;
22    age : byte;
23    gender : GENDER_TYPE;
24    phone_num : ulong;
25}
26 
27table personal_info_list
28{
29    info : [personal_info];
30}
31 
32root_type personal_info_list;




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

01//////////////////////////////////////////////////////
02//// FILE     : tellist.proto
03//// DESC     : basic message for msg-center
04//// AUTHOR   : v 0.1 written by Alex Stocks on June 22, 2014
05//// LICENCE  :
06//// MOD      :
07////////////////////////////////////////////////////////
08 
09package as.tellist;
10 
11enum gender_type
12{
13    MALE    = 0;
14    FEMALE  = 1;
15    OTHER   = 2;
16}
17 
18message personal_info
19{
20    optional uint32         id = 1;
21    optional string         name = 2;
22    optional uint32         age = 3;
23    optional gender_type    gender = 4;
24    optional uint64         phone_num = 5;
25}
26 
27message personal_info_list
28{
29    repeated personal_info  info = 1;
30}


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

    测试结果如下:

01测试环境:12Core Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
02free
03             total       used       free     shared    buffers     cached
04Mem:      66081944   65831100     250844          0     182240   46903452
05-/+ buffers/cache:   18745408   47336536
06Swap:       975864     724648     251216
07 
08protobuf三次测试结果:
09./bin/tellist_test
10loop = 1000000, time diff = 14283ms
11buf size:841
12 
13bin/tellist_test
14loop = 1000000, time diff = 14096ms
15buf size:841
16 
17bin/tellist_test
18loop = 1000000, time diff = 14229ms
19buf size:841
20占用内存空间841Byte,平均运算时间42608ms / 3 = 14202.7ms
21 
22flatbuffers三次测试结果:
23bin/tellist_test
24loop = 1000000, time diff = 11694ms
25buf size:1712
26 
27 bin/tellist_test
28loop = 1000000, time diff = 11710ms
29buf size:1712
30 
31bin/tellist_test
32loop = 1000000, time diff = 11774ms
33buf size:1712
34占用内存空间1712Byte,平均运算时间35178ms / 3 = 11726ms
35 
36 
37测试环境:1 Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
38MEM
39             total       used       free     shared    buffers     cached
40Mem:        753932     432672     321260          0      89236     258052
41-/+ buffers/cache:      85384     668548
42Swap:      1324028          0    1324028
43 
44protobuf三次测试结果:
45bin/tellist_test
46loop = 1000000, time diff = 12779ms
47buf size:841
48 
49bin/tellist_test
50loop = 1000000, time diff = 13475ms
51buf size:841
52 
53bin/tellist_test
54loop = 1000000, time diff = 12604ms
55buf size:841
56占用内存空间841Byte,平均运算时间38858ms / 3 = 12952.7ms
57 
58flatbuffers三次测试结果:
59bin/tellist_test
60loop = 1000000, time diff = 9424ms
61buf size:1712
62 
63 bin/tellist_test
64loop = 1000000, time diff = 9277ms
65buf size:1712
66 
67bin/tellist_test
68loop = 1000000, time diff = 9265ms
69buf size:1712
70info vecotor size:37, its right size:37
71占用内存空间1712Byte,平均运算时间28036ms / 3 = 9345ms

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

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

    测试程序已经上传到百度网盘,点击这个链接即可下载。欢迎各位的批评意见。