对比MessagePack和Protocal Buffer

原文: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



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值