Google Protocol Buffer举例1
??? 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;?
}
下面是上述行用/出/序列化 的C++程序示例:
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);
入/以字符串入的C++程序示例:
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);
上面的例子我可以看到, Protocol Buffer上就是 C++面的一型, 所以它的行速度比起 XML文件快上非常大的倍.? 因XML 需要文件中取出字符串,再成 XML 文象模型, 然後再XML 文象模型中取出指定的字符串, 最後再字符串成指定型的量, 繁的理, 大大消耗 CPU 源. 而Protocol Buffer只需要地一二制序列, 按照指定的格式取到 C++ 型中去就行了.