protobuf json xml比较

1 protobuf/xml/json对比json

从数据的存储格式的角度进行对比google

假如要存储一个键值对:xml

{price:150}对象

1.1 protobuf的表示方式io

message  Test {ast

    optional int32 price = 1;coding

}序列化

protobuf的物理存储:08 96 01,就3个字节。developer

采用key-value的方式存放,第一个字节是key,它是field_number << 3 | wire_type构成。数据

因此field number是1,wire type是0,即varint,有了这个wire type就能够用来解析96 01了。

96 01 = 1001 0110 0000 0001

即001 0110 000 0001

least significant first

1001 0110 = 128 + 16 + 4 + 2 = 150.

只要3 bytes

1.2 xml的存储表示

<some>

  <name>price</name>

  <value>150</value>

</some>

大约要36 bytes

1.3 json的存储表示

{price:150}

大约11bytes

 

比较可见相比于json和xml,protobuf对象序列化时能够节省很是大的空间,从而带来很是快的传输速度。

另外因为protobuf表示简单,解析速度也更快。

 

参考

1 https://developers.google.com/protocol-buffers/docs/encoding

相关文章
相关标签/搜索