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文件例如以下:

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

由于要以protobuf做性能參考。列出protobuf的idl文件例如以下:

  1. //////////////////////////////////////////////////////
  2. //// FILE : tellist.proto
  3. //// DESC : basic message for msg-center
  4. //// AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014
  5. //// LICENCE :
  6. //// MOD :
  7. ////////////////////////////////////////////////////////
  8.  
  9. package as.tellist;
  10.  
  11. enum gender_type
  12. {
  13. MALE = 0;
  14. FEMALE = 1;
  15. OTHER = 2;
  16. }
  17.  
  18. message 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.  
  27. message personal_info_list
  28. {
  29. repeated personal_info info = 1;
  30. }

若用C的struct描写叙述相应的头文件(其相应的程序称之为“二进制”)。例如以下:

  1. /**
  2.  * FILE : tellist.h
  3.  * DESC : to test tellist
  4.  * AUTHOR : v1.0 written by Alex Stocks
  5.  * DATE : on June 28, 2014
  6.  * LICENCE : GPL 2.0
  7.  * MOD :
  8.  **/
  9.  
  10. #ifndef __TELLIST_H__
  11. #define __TELLIST_H__
  12.  
  13. enum
  14. {
  15. GENDER_TYPE_MALE = 0,
  16. GENDER_TYPE_FEMALE = 1,
  17. GENDER_TYPE_OTHER = 2,
  18. };
  19.  
  20. inline const char **EnumNamesGENDER_TYPE()
  21. {
  22. static const char *names[] = { "MALE", "FEMALE", "OTHER"};
  23. return names;
  24. }
  25.  
  26. inline const char *EnumNameGENDER_TYPE(int e)
  27. {
  28. return EnumNamesGENDER_TYPE()[e];
  29. }
  30.  
  31. typedef struct personal_info_tag
  32. {
  33. unsigned id;
  34. unsigned char age;
  35. char gender;
  36. unsigned long long phone_num;
  37. char name[32];
  38. } personal_info;
  39.  
  40. typedef struct personal_info_list_tag
  41. {
  42. int size;
  43. personal_info info[0];
  44. } personal_info_list;
  45.  
  46. #endif
  47.  
  48. // the end of the header file tellist.h

測试时,在内存中构造37个personal_info对象。并序列化之,反复这个过程100万次。然后再进行反序列化,再反复100万次。

測试结果例如以下(补充:tellist_pb是protobuf測试程序,tellist_fb是FlatBuffers測试程序,tellist_fb是二进制測试程序。):

  1. 測试环境:12Core Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
  2. free
  3.              total       used       free     shared    buffers     cached
  4. Mem:      66081944   62222028    3859916          0     196448   43690828
  5. -/+ buffers/cache:   18334752   47747192
  6. Swap:       975864     855380     120484
  7.  
  8. protobuf三次測试结果:
  9. bin/tellist_pb 
  10. encode: loop = 1000000, time diff = 14210ms
  11. decode: loop = 1000000, time diff = 11185ms
  12. buf size:841
  13.  
  14. bin/tellist_pb 
  15. encode: loop = 1000000, time diff = 14100ms
  16. decode: loop = 1000000, time diff = 11234ms
  17. buf size:841
  18.  
  19. bin/tellist_pb 
  20. encode: loop = 1000000, time diff = 14145ms
  21. decode: loop = 1000000, time diff = 11237ms
  22. buf size:841
  23. 序列化后占用内存空间841Byteencode平均运算时间42455ms / 3 = 14151.7msdecode平均计算时间33656ms / 3 = 11218.7ms
  24.  
  25. flatbuffers三次測试结果:
  26.  bin/tellist_fb 
  27. encode: loop = 1000000, time diff = 11666ms
  28. decode: loop = 1000000, time diff = 1141ms
  29. buf size:1712
  30.  
  31. bin/tellist_fb 
  32. encode: loop = 1000000, time diff = 11539ms
  33. decode: loop = 1000000, time diff = 1200ms
  34. buf size:1712
  35.  
  36. bin/tellist_fb 
  37. encode: loop = 1000000, time diff = 11737ms
  38. decode: loop = 1000000, time diff = 1141ms
  39. buf size:1712
  40. 序列化后占用内存空间1712Byteencode平均运算时间34942ms / 3 = 11647.3msdecode平均计算时间3482ms / 3 = 1160.7ms
  41.  
  42. 二进制三次測试结果:
  43. bin/tellist 
  44. encode: loop = 1000000, time diff = 4967ms
  45. decode: loop = 1000000, time diff = 688ms
  46. buf size:304
  47.  
  48.  bin/tellist 
  49. encode: loop = 1000000, time diff = 4971ms
  50. decode: loop = 1000000, time diff = 687ms
  51. buf size:304
  52.  
  53. bin/tellist 
  54. encode: loop = 1000000, time diff = 4966ms
  55. decode: loop = 1000000, time diff = 686ms
  56. buf size:304
  57. 序列化后占用内存空间304Byteencode平均运算时间14904ms / 3 = 4968msdecode平均计算时间2061ms / 3 = 687ms
  58.  
  59. 測试环境:1 Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
  60. free
  61.              total       used       free     shared    buffers     cached
  62. Mem:        753932     356036     397896          0      50484     224848
  63. -/+ buffers/cache:      80704     673228
  64. Swap:      1324028        344    1323684
  65. protobuf三次測试结果:
  66. ./bin/tellist_pb 
  67. encode: loop = 1000000, time diff = 12451ms
  68. decode: loop = 1000000, time diff = 9662ms
  69. buf size:841
  70.  
  71. ./bin/tellist_pb 
  72. encode: loop = 1000000, time diff = 12545ms
  73. decode: loop = 1000000, time diff = 9840ms
  74. buf size:841
  75.  
  76. ./bin/tellist_pb 
  77. encode: loop = 1000000, time diff = 12554ms
  78. decode: loop = 1000000, time diff = 10460ms
  79. buf size:841
  80. 序列化后占用内存空间841Byteencode平均运算时间37550ms / 3 = 12516.7msdecode平均计算时间29962ms / 3 = 9987.3ms
  81.  
  82. flatbuffers三次測试结果:
  83. bin/tellist_fb 
  84. encode: loop = 1000000, time diff = 9640ms
  85. decode: loop = 1000000, time diff = 1164ms
  86. buf size:1712
  87.  
  88. bin/tellist_fb 
  89. encode: loop = 1000000, time diff = 9595ms
  90. decode: loop = 1000000, time diff = 1170ms
  91. buf size:1712
  92.  
  93. bin/tellist_fb 
  94. encode: loop = 1000000, time diff = 9570ms
  95. decode: loop = 1000000, time diff = 1172ms
  96. buf size:1712
  97. 序列化后占用内存空间1712Byteencode平均运算时间28805ms / 3 = 9345msdecode平均计算时间3506ms / 3 = 1168.7ms
  98.  
  99. 二进制三次測试结果:
  100. bin/tellist 
  101. encode: loop = 1000000, time diff = 4194ms
  102. decode: loop = 1000000, time diff = 538ms
  103. buf size:304
  104.  
  105. bin/tellist 
  106. encode: loop = 1000000, time diff = 4387ms
  107. decode: loop = 1000000, time diff = 544ms
  108. buf size:304
  109.  
  110. bin/tellist 
  111. encode: loop = 1000000, time diff = 4181ms
  112. decode: loop = 1000000, time diff = 533ms
  113. buf size:304
  114. 序列化后占用内存空间304Byteencode平均运算时间12762ms / 3 = 4254msdecode平均计算时间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性能比較的更多相关文章

  1. GJM : FlatBuffers 与 protobuf 性能比较 [转载 ]

    原帖地址:http://blog.csdn.net/menggucaoyuan/article/details/34409433 原作者:企鹅  menggucaoyuan 未经原作者同意不允许转载 ...

  2. FlatBuffers与protobuf性能比较

    FlatBuffers发布时,顺便也公布了它的性能数据,具体数据请见Benchmark. 它的测试用例由以下数据构成"a set of about 10 objects containing ...

  3. Java中arraylist和linkedlist源代码分析与性能比較

    Java中arraylist和linkedlist源代码分析与性能比較 1,简单介绍 在java开发中比較经常使用的数据结构是arraylist和linkedlist,本文主要从源代码角度分析arra ...

  4. Java序列化框架性能比較

    博客: http://colobu.com jvm-serializers提供了一个非常好的比較各种Java序列化的的測试套件. 它罗列了各种序列化框架. 能够自己主动生成測试报告. 我在AWS c3 ...

  5. Java里多个Map的性能比較(TreeMap、HashMap、ConcurrentSkipListMap)

    问题 比較Java原生的 1.  TreeMap 2.  HashMap 3.  ConcurrentSkipListMap 3种Map的效率. 结果 模拟150W以内海量数据的插入和查找,通过添加和 ...

  6. createElement与innerHtml性能比較

    js中.动态加入html的方法大致就是两种,一种是document.createElement等方法创建,然后使用Element.appendChild加入,或者是使用Element.innerHTM ...

  7. [轉]redis;mongodb;memcache三者的性能比較

    先说我自己用的情况: 最先用的memcache ,用于键值对关系的服务器端缓存,用于存储一些常用的不是很大,但需要快速反应的数据 然后,在另一个地方,要用到redis,然后就去研究了下redis. 一 ...

  8. java list三种遍历方法性能比較

    从c/c++语言转向java开发,学习java语言list遍历的三种方法,顺便測试各种遍历方法的性能,測试方法为在ArrayList中插入1千万条记录,然后遍历ArrayList,发现了一个奇怪的现象 ...

  9. Google序列化库FlatBuffers 1.1发布,及与protobuf的比较

    个人总结: FlatBuffer相对于Protobuffer来讲,优势如下: 1. 由于省去了编解码的过程,所以从速度上快于Protobuffer,个人测试结果100w次编解码,编码上FlatBuff ...

随机推荐

  1. Educational Codeforces Round 12 A. Buses Between Cities 水题

    A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...

  2. css中!important的优先级问题

    css中!important的优先级在主页面中写>在外部引用的css文件 之前我一直以为css的样式不管写在哪里只要加上!important那么它的优先级就是最高的,事实上并不是这样的,尤其在动 ...

  3. JSoup 用法详解

    清单 1 // 直接从字符串中输入 HTML 文档 String html = "<html><head><title> 开源中国社区 </titl ...

  4. ADC for programmable logic uses one capacitor

    Many electronic devices require user input for setting the application properties. Typical input dev ...

  5. 解决/dev/fb0无法打开的问题

    最近要在Linux做基于frame Buffer的图形显示,不论我在独立分区的Linux FC6系统中,还是在装有Red hat9的VPC中,都无法打开/dev/fb0.从网上找了很多资料,都没能解决 ...

  6. 通过 WCF 实现点对点文件共享 z

    下载免费的项目源代码 下载项目的数据库 目录 简介 背景 为什么是WCF? WCF历史简述 WCF基础 点对点概念 代码分析(它是怎么工作的) 核心转化引擎层 下载管理层 服务层 代码的使用(如何运行 ...

  7. JSP如何导入ckeditor

    <textarea rows="3" cols="100" id="editor1"></textarea> < ...

  8. JAVA单例MongoDB工具类

    我经常对MongoDB进行一些基础操作,将这些常用操作合并到一个工具类中,方便自己开发使用. 没用Spring Data.Morphia等框架是为了减少学习.维护成本,另外自己直接JDBC方式的话可以 ...

  9. @Spring MVC 中几种获取request和response的方式

    1.最简单方式:处理方法入参 例如: @RequestMapping("/test") @ResponseBody public void saveTest(HttpServlet ...

  10. c++学习之多态(虚函数和纯虚函数)

    c++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...