在目前用戶端開發中, pb 格式已經是網絡傳輸的序列化/反序列化方案的标配。但 pb 同時對我們 native 開發中的緩存實作也是有很大價值的。
本人對比了如下幾種用戶端的緩存方案:
pb + disklrucache
ormlite/sqlite
gson + disklrucache
其中 pb + disklrucache 方案與 gson + disklrucache 方案的性能對比如下:
實測資料:
json write
pb write
json read
pb read
35
10
110
45
29
5
87
6
36
72
39
85
分項加總後:
139
25
354
61
可以看出寫操作,pb 所花時間僅占 json 的18%;讀操作,pb 所花時間僅占 json 的17%。此外,ormlite/sqlite 在性能方面與 json 相當。
既然好處是明顯的,我們看下如何應用這項新技巧,直接上代碼:
别忘了添加 api 包依賴: