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;
}
若用C的struct描写叙述相应的头文件(其相应的程序称之为“二进制”)。例如以下:
/**
* FILE : tellist.h
* DESC : to test tellist
* AUTHOR : v1.0 written by Alex Stocks
* DATE : on June 28, 2014
* LICENCE : GPL 2.0
* MOD :
**/ #ifndef __TELLIST_H__
#define __TELLIST_H__ enum
{
GENDER_TYPE_MALE = 0,
GENDER_TYPE_FEMALE = 1,
GENDER_TYPE_OTHER = 2,
}; inline const char **EnumNamesGENDER_TYPE()
{
static const char *names[] = { "MALE", "FEMALE", "OTHER"};
return names;
} inline const char *EnumNameGENDER_TYPE(int e)
{
return EnumNamesGENDER_TYPE()[e];
} typedef struct personal_info_tag
{
unsigned id;
unsigned char age;
char gender;
unsigned long long phone_num;
char name[32];
} personal_info; typedef struct personal_info_list_tag
{
int size;
personal_info info[0];
} personal_info_list; #endif // the end of the header file tellist.h
測试时,在内存中构造37个personal_info对象。并序列化之,反复这个过程100万次。然后再进行反序列化,再反复100万次。
測试结果例如以下(补充:tellist_pb是protobuf測试程序,tellist_fb是FlatBuffers測试程序,tellist_fb是二进制測试程序。):
測试环境:12Core Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
free
total used free shared buffers cached
Mem: 66081944 62222028 3859916 0 196448 43690828
-/+ buffers/cache: 18334752 47747192
Swap: 975864 855380 120484 protobuf三次測试结果:
bin/tellist_pb
encode: loop = 1000000, time diff = 14210ms
decode: loop = 1000000, time diff = 11185ms
buf size:841 bin/tellist_pb
encode: loop = 1000000, time diff = 14100ms
decode: loop = 1000000, time diff = 11234ms
buf size:841 bin/tellist_pb
encode: loop = 1000000, time diff = 14145ms
decode: loop = 1000000, time diff = 11237ms
buf size:841
序列化后占用内存空间841Byte,encode平均运算时间42455ms / 3 = 14151.7ms,decode平均计算时间33656ms / 3 = 11218.7ms flatbuffers三次測试结果:
bin/tellist_fb
encode: loop = 1000000, time diff = 11666ms
decode: loop = 1000000, time diff = 1141ms
buf size:1712 bin/tellist_fb
encode: loop = 1000000, time diff = 11539ms
decode: loop = 1000000, time diff = 1200ms
buf size:1712 bin/tellist_fb
encode: loop = 1000000, time diff = 11737ms
decode: loop = 1000000, time diff = 1141ms
buf size:1712
序列化后占用内存空间1712Byte,encode平均运算时间34942ms / 3 = 11647.3ms,decode平均计算时间3482ms / 3 = 1160.7ms 二进制三次測试结果:
bin/tellist
encode: loop = 1000000, time diff = 4967ms
decode: loop = 1000000, time diff = 688ms
buf size:304 bin/tellist
encode: loop = 1000000, time diff = 4971ms
decode: loop = 1000000, time diff = 687ms
buf size:304 bin/tellist
encode: loop = 1000000, time diff = 4966ms
decode: loop = 1000000, time diff = 686ms
buf size:304
序列化后占用内存空间304Byte。encode平均运算时间14904ms / 3 = 4968ms,decode平均计算时间2061ms / 3 = 687ms 測试环境:1 Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
free
total used free shared buffers cached
Mem: 753932 356036 397896 0 50484 224848
-/+ buffers/cache: 80704 673228
Swap: 1324028 344 1323684
protobuf三次測试结果:
./bin/tellist_pb
encode: loop = 1000000, time diff = 12451ms
decode: loop = 1000000, time diff = 9662ms
buf size:841 ./bin/tellist_pb
encode: loop = 1000000, time diff = 12545ms
decode: loop = 1000000, time diff = 9840ms
buf size:841 ./bin/tellist_pb
encode: loop = 1000000, time diff = 12554ms
decode: loop = 1000000, time diff = 10460ms
buf size:841
序列化后占用内存空间841Byte,encode平均运算时间37550ms / 3 = 12516.7ms,decode平均计算时间29962ms / 3 = 9987.3ms flatbuffers三次測试结果:
bin/tellist_fb
encode: loop = 1000000, time diff = 9640ms
decode: loop = 1000000, time diff = 1164ms
buf size:1712 bin/tellist_fb
encode: loop = 1000000, time diff = 9595ms
decode: loop = 1000000, time diff = 1170ms
buf size:1712 bin/tellist_fb
encode: loop = 1000000, time diff = 9570ms
decode: loop = 1000000, time diff = 1172ms
buf size:1712
序列化后占用内存空间1712Byte,encode平均运算时间28805ms / 3 = 9345ms,decode平均计算时间3506ms / 3 = 1168.7ms 二进制三次測试结果:
bin/tellist
encode: loop = 1000000, time diff = 4194ms
decode: loop = 1000000, time diff = 538ms
buf size:304 bin/tellist
encode: loop = 1000000, time diff = 4387ms
decode: loop = 1000000, time diff = 544ms
buf size:304 bin/tellist
encode: loop = 1000000, time diff = 4181ms
decode: loop = 1000000, time diff = 533ms
buf size:304
序列化后占用内存空间304Byte,encode平均运算时间12762ms / 3 = 4254ms,decode平均计算时间1615ms / 3 = 538.3ms
上面的二进制程序的结果不管在内存空间占用还是cpu计算时间这两个指标上都是最快的。但本文仅仅讨论FlatBuffers和protobuf。所以不让它的结果參与比較。
从以上数据看出。在内存空间占用这个指标上。FlatBuffers占用的内存空间比protobuf多了两倍。序列化时二者的cpu计算时间FB比PB快了3000ms左右,反序列化时二者的cpu计算时间FB比PB快了9000ms左右。FB在计算时间上占优势,而PB则在内存空间上占优(相比FB。这也正是它计算时间比較慢的原因)。
上面的測试环境是在公司的linux server端和我自己的mac pro分别进行的。请手机端开发人员自己也在手机端进行下測试, 应该能得到类似的结果。
Google宣称FB适合游戏开发是有道理的,假设在乎计算时间我想它也适用于后台开发。
另外,FB大量使用了C++11的语法。其从idl生成的代码接口不如protubuf友好。只是相比使用protobuf时的一堆头文件和占18M之多的lib库,FlatBuffers只一个"flatbuffers/flatbuffers.h"就足够了。
測试程序已经上传到百度网盘,点击这个链接就可以下载。
欢迎各位的批评意见。
FlatBuffers与protobuf性能比較的更多相关文章
- GJM : FlatBuffers 与 protobuf 性能比较 [转载 ]
原帖地址:http://blog.csdn.net/menggucaoyuan/article/details/34409433 原作者:企鹅 menggucaoyuan 未经原作者同意不允许转载 ...
- FlatBuffers与protobuf性能比较
FlatBuffers发布时,顺便也公布了它的性能数据,具体数据请见Benchmark. 它的测试用例由以下数据构成"a set of about 10 objects containing ...
- Java中arraylist和linkedlist源代码分析与性能比較
Java中arraylist和linkedlist源代码分析与性能比較 1,简单介绍 在java开发中比較经常使用的数据结构是arraylist和linkedlist,本文主要从源代码角度分析arra ...
- Java序列化框架性能比較
博客: http://colobu.com jvm-serializers提供了一个非常好的比較各种Java序列化的的測试套件. 它罗列了各种序列化框架. 能够自己主动生成測试报告. 我在AWS c3 ...
- Java里多个Map的性能比較(TreeMap、HashMap、ConcurrentSkipListMap)
问题 比較Java原生的 1. TreeMap 2. HashMap 3. ConcurrentSkipListMap 3种Map的效率. 结果 模拟150W以内海量数据的插入和查找,通过添加和 ...
- createElement与innerHtml性能比較
js中.动态加入html的方法大致就是两种,一种是document.createElement等方法创建,然后使用Element.appendChild加入,或者是使用Element.innerHTM ...
- [轉]redis;mongodb;memcache三者的性能比較
先说我自己用的情况: 最先用的memcache ,用于键值对关系的服务器端缓存,用于存储一些常用的不是很大,但需要快速反应的数据 然后,在另一个地方,要用到redis,然后就去研究了下redis. 一 ...
- java list三种遍历方法性能比較
从c/c++语言转向java开发,学习java语言list遍历的三种方法,顺便測试各种遍历方法的性能,測试方法为在ArrayList中插入1千万条记录,然后遍历ArrayList,发现了一个奇怪的现象 ...
- Google序列化库FlatBuffers 1.1发布,及与protobuf的比较
个人总结: FlatBuffer相对于Protobuffer来讲,优势如下: 1. 由于省去了编解码的过程,所以从速度上快于Protobuffer,个人测试结果100w次编解码,编码上FlatBuff ...
随机推荐
- mybatis源码分析(7)-----缓存Cache(一级缓存,二级缓存)
写在前面 MyBatis 提供查询缓存,用于减轻数据库压力,提高数据库性能. MyBatis缓存分为一级缓存和二级缓存. 通过对于Executor 的设计.也可以发现MyBatis的缓存机制(采用模 ...
- Scrapy 轻松定制网络爬虫(转)
网络爬虫(Web Crawler, Spider)就是一个在网络上乱爬的机器人.当然它通常并不是一个实体的机器人,因为网络本身也是虚拟的东西,所以这个“机器人”其实也就是一段程序,并且它也不是乱爬,而 ...
- ios/object-c中的UIColor一些比较偏的颜色展示/示例
UIColor blackColor]; [UIColor darkGrayColor]; [UIColor lightGrayColor]; [UIColor grayColor]; [UIColo ...
- Maven:Generating Project in Batch mode 卡住问题
Maven命令执行到Generating Project in Batch mode 卡住,原因是网络带宽不足问题!需要下载一个约4.1M的archetype-catalog.xml文件. Maven ...
- Android 新浪博客分享问题总结
分类: android(33) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 我遇到BUG 1 sso package or singn error Bug2 ...
- iOS 改动toolbar里面文字的字体和大小
使用NSDictionaty来设置文本的属性: NSDictionary * attributes = @{NSFontAttributeName: [UIFont fontWithName:@&qu ...
- Enable Notepad++ 666 support both SCLEX_FORTRAN and SCLEX_F77
Enable Notepad++ 666 support both SCLEX_FORTRAN and SCLEX_F77 http://blog.csdn.net/gocad/article/det ...
- .NET:负载平衡的主意事项
允许局域网发现和共享. 设置固定IP. 网站的IP设置为“全部未分配”. 注意:如果停止IIS的话,不会对负载平衡有影响,负载还是会分配停止了的IIS所在在的电脑,只有停止服务器了,负载不会再分配给停 ...
- 彻底删除SVN版本库中部分文件或文件夹
例:假设SVN库路径为E:/svn/project,库中的目录结构为 QA/Trunk Software/Tags/test.exe 删除Software/Tags/目录下的test.exe文件 操作 ...
- HTML学习要点
目标 掌握HTML基本语法,了解HTML Document结构,能熟练使用HTML Element对象. 要点 基本概念:什么是HTML.HTML标签? 熟悉常用的HTML标签含义以及应用场合. ht ...