Protocol Buffers (a.k.a., protobuf) are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data.

Protobuf is a language neutral platform neutral extensible serialization mechanism for structured data developed by Google.

This paper mainly compares the performance of Protobuf and JSON serialization and deserialization. JSON serialization uses Google’s official Gson framework.

Protobuf Gson Language Platform
3.17.3 2.8.7 Kotlin macOS IntelliJ IDEA

Test serialized content, work efficiently with 25 minutes of mock data

The data structure

syntax = "proto3";
package me.sunnyxibei.data;
option java_package = "me.sunnyxibei.data";
option java_outer_classname = "TaskProto";

message  Eeg{
  repeated double  alphaData = 1;
  repeated double  betaData = 2;
  repeated double  attentionData = 3;
  repeated int64  timestampData = 4;
  int64  startTimestamp = 5;
  int64  endTimestamp = 6;
}
message TaskRecord{
  string localId = 1;
  int64  localCreated = 2;
  int64  localUpdated = 3;
  int32 score = 4;
  int64  originDuration = 5;
  string  subject = 6;
  string  content = 7;
  Eeg eeg = 8;
}
Copy the code

Comparison result repeat = 1

Gson serialization size = 30518 bytes Gson serialization time = 113 ms Protobuf serialization size = 13590 bytes Protobuf serialization time = 39 ms ************************* Gson deserialization time = 15 ms Protobuf deserialization time = 3 msCopy the code

repeat = 10

Gson serialization time = 137 ms Protobuf serialization time = 41 ms ************************* Gson deserialization time = 50 ms Protobuf deserialization time = 5 msCopy the code

repeat = 100

Gson serialization time = 347 ms Protobuf serialization time = 47 ms ************************* Gson deserialization time = 212 ms Protobuf deserialization time = 22 msCopy the code

repeat = 1000

Gson serialization time = 984 ms Protobuf serialization time = 97 ms ************************* Gson deserialization time = 817 ms Protobuf deserialization time = 105 msCopy the code

repeat = 10000

Gson serialization time = 7034 ms Protobuf serialization time = 225 ms ************************* Gson deserialization time = 5544 ms Protobuf deserialization time = 300 msCopy the code

repeat = 100000

Gson serialization time = 65560 ms Protobuf serialization time = 1469 ms ************************* Gson deserialization time = 49984 ms Protobuf deserialization time = 2409 msCopy the code

Conclusion:

  1. For spatial comparison, Protobuf serialized data size is 44.5% of JSON serialized data size
  2. Time to compare
The number of Serialization (Protobuf/JSON) Deserialization (Protobuf/JSON)
1 34.5% 20%
10 29.9% 10%
100 13.5% 9.43%
1000 9.9% 12.9%
10000 3.2% 5.41%
100000 2.24% 4.82%