1. 程式人生 > >protobuf json xml比較

protobuf json xml比較

TP bsp 第一個 col 一個 CA protocol PE 快的

1 protobuf/xml/json對比

從數據的存儲格式的角度進行對比

假如要存儲一個鍵值對:

{price:150}

1.1 protobuf的表示方式

message Test {

optional int32 price = 1;

}

protobuf的物理存儲:08 96 01,就3個字節。

采用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

protobuf json xml比較