对比MessagePack和Protocal Buffer
来源:互联网 发布:linux 禁止用户登录 编辑:程序博客网 时间:2024/06/05 08:19
原文:http://www.igvita.com/2011/08/01/protocol-buffers-avro-thrift-messagepack/
"The Best" Serialization Format
Reflecting on the use of Protocol Buffers at Google and all of the above competitors it is clear that there is no one definitive, "best" option. Rather, each solution makes perfect sense in the context it was developed and hence the same logic should be applied to your own situation.
If you are looking for a battle-tested, strongly typed serialization format, then Protocol Buffers is a great choice. If you also need a variety of built-in RPC mechanisms, then Thrift is worth investigating. If you are already exchanging or working with JSON, then MessagePack is almost a drop-in optimization. And finally, if you like the strongly typed aspects, but want the flexibility of easy interoperability with dynamic languages, then Avro may be your best bet at this point in time.
网上看到微博说MessagePack多厉害,性能是Protocal Buffer的4倍,学习了下,应该没有传说中的神奇,下面是MessagePack的一个例子:
#include <msgpack.hpp>#include <vector>#include <string> class myclass {private: std::string m_str; std::vector<int> m_vec;public: MSGPACK_DEFINE(m_str, m_vec);}; int main(void) { std::vector<myclass> vec; // add some elements into vec... // you can serialize myclass directly msgpack::sbuffer sbuf; msgpack::pack(sbuf, vec); msgpack::unpacked msg; msgpack::unpack(&msg, sbuf.data(), sbuf.size()); msgpack::object obj = msg.get(); // you can convert object to myclass directly std::vector<myclass> rvec; obj.convert(&rvec);}
对于大型程序它的这种接口过于简单,无法加入各种控制或者选项(比如某些数据有些时候需要序列化,有些时候不需要序列化)导致实际不可用。但是对于普通小程序,MessagePack这种就特别好用了,序列化、反序列化细节无需关注,只管pack unpack即可。
另外,MessagePack还有一个对于小公司特别有利的特性:它支持跨语言序列化反序列化。Ruby pack,C++ unpack,直接搞定,非常方便。
还有一个特别想说的,MessagePack的帮助网站做得特别出色,类目清晰入门迅速。不妨去看看:
main:http://msgpack.org/
help:http://wiki.msgpack.org/display/MSGPACK/Home
Protocol Buffer需要额外写一个Schema:
message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4;}然后再利用代码:
Person person;person.set_name("John Doe");person.set_id(1234);person.set_email("jdoe@example.com");fstream output("myfile", ios::out | ios::binary);person.SerializeToOstream(&output);// Then, later on, you could read your message back in:fstream input("myfile", ios::in | ios::binary);Person person;person.ParseFromIstream(&input);cout << "Name: " << person.name() << endl;cout << "E-mail: " << person.email() << endl;
-
There is no magic but code.
- by Raywill
- 对比MessagePack和Protocal Buffer
- Protocal Buffer
- protocal buffer中编码和解码过程
- Google Protocol buffer 系列一: 对比 Protocal Buffers, Thrift, Avro
- protocal buffer详解
- protocal buffer vs hessian
- protocal buffer详解
- Google Protocal Buffer
- protocal buffer repeate 关键字
- MessagePack 编码和解码
- Protocal Buffer 2.3.0使用注意事项
- Facebook Wire Protocal Buffer (protobuf)简介
- spring boot restTemplate protobuf(protocal buffer) demo
- protocal buffer的一些基本整理
- Protocal
- JSON,MessagePack导出和读取
- nodejs测试: buffer和字符串相加对比测试
- ZMQ和MessagePack的简单使用
- 使用过的Makefile例子
- hash_map一些纠结的问题
- Windows RT和Windows 8有什么区别
- ActiveMQ学习(一)——MQ的基本概念
- 查询用户所有表
- 对比MessagePack和Protocal Buffer
- 算法之美——八皇后
- Linux环境变量
- Linux下DNS域名解析的配置文件 resolv.conf
- java线程学习3——线程的停止
- 修改浏览器图标为自己想要的LOGO
- Binder驱动
- 马士兵Spring学习笔记
- <s:action>的用法