When flatbuffers was released, it also published its performance data. For details, see benchmark.
Its test case consists of the following data: "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."
I felt like a kid, so I designed a test case, focusing on two indicators: CPU computing time and memory space usage. The reference object is protobuf.
The test case is: serialize an address book personal_info_list (table). The address book can be considered as a set of personal information. Personal_info (table): Personal ID (uint), name (string), age (byte), gender (Enum, byte), and phone number (ulong ). Originally, I wanted to use struct to represent personal_info (table). However, struct does not allow arrays or string members. Instead, I can use table to describe it. The corresponding IDL file is as follows:
////////////////////////////////////////////////////////// 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;
To use protobuf for Performance Reference, list the IDL files of protobuf as follows:
////////////////////////////////////////////////////////// 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 uint32id = 1;optional stringname = 2;optional uint32age = 3;optional gender_typegender = 4;optional uint64phone_num = 5;}message personal_info_list{repeated personal_infoinfo = 1;}
Construct 37 personal_info objects in the memory and serialize them. repeat this process for 1 million times.
The test results are as follows:
Test environment: 12 core Intel (r) Xeon (r) CPU E5-2620 0 @ 2.00 GHz free total used free shared buffers cachedmem: 66081944 65831100 250844 182240 46903452-/+ buffers/cache: 18745408 47336536 swap: 975864 724648 251216protobuf three test results :. /bin/tellist_test loop = 1000000, time diff = 14283 msbuf size: 841bin/tellist_test loop = 1000000, time diff = 14096 msbuf size: 841bin/tellist_test loop = 1000000, time diff = 14229 msbuf size: 841 memory space occupied by 841 bytes, average computing time 42608 ms/3 = 14202.7msflatbuffers test results: Bin/tellist_test loop = 1000000, time diff = 11694 msbuf size: 1712 bin/tellist_test loop = 1000000, time diff = 11710 msbuf size: 1712bin/tellist_test loop = 1000000, time diff = 11774 msbuf size: 1712 memory space occupied 1712 bytes, average computing time 35178 ms/3 = 11108ms test environment: 1 core Intel (r) core (TM) i5-3210M CPU @ 2.50 ghzmem total used free shared buffers cachedmem: 753932 432672 321260 0 89236-/+ buffers/cache: 258052 85384 668548 swap: 1324028 0 1324028protobuf three test results: bin/tellist_test loop = 1000000, time diff = 12779 msbuf size: 841bin/tellist_test loop = 1000000, time diff = 13475 msbuf size: 841bin/tellist_test loop = 1000000, time diff = 12604 msbuf size: 841 memory space occupied by 841 bytes, average computing time 38858 ms/3 = 12952.7msflatbuffers test results: Bin/tellist_test loop = 1000000, time diff = 9424 msbuf size: 1712 bin/tellist_test loop = 1000000, time diff = 9277 msbuf size: 1712bin/tellist_test loop = 1000000, time diff = 9265 msbuf size: 1712 info vecotor size: 37, its right size: 37 occupies 1712 bytes of memory space, and the average computing time is 28036 ms/3 = 9345 Ms
From the above data, we can see that flatbuffers occupies twice more memory space than protobuf in terms of memory space usage, while the CPU computing time of the two is about Ms different, however, considering that 1 million tests were used, there was almost no difference in the computing time between the two.
From the above test data, the performance of flatbuffers is not as good as it boast, and I am a little disappointed. FB uses a lot of C ++ 11 syntax, and its code interfaces generated from IDL are not as friendly as protubuf. However, compared with a bunch of header files used in protobuf and a lib library that accounts for 18 MB, flatbuffers is only one "flatbuffers/flatbuffers. H.
The test program has been uploaded to the Baidu online storage. Click this link to download the program. All comments are welcome.