C#中使用ProtoBuf提高序列化速度对比二进制序列化
場景
ProtoBuf
protocolbuffer是google 的一種數據交換的格式,它獨立于語言,獨立于平臺。
google 提供了多種語言的實現:java、c#、c++、go 和 python,每一種實現都包含了相應語言的編譯器以及庫文件。
由于它是一種二進制的格式,比使用xml 進行數據交換快許多??梢园阉糜诜植际綉弥g的數據通信或者異構環境下的數據交換。
作為一種效率和兼容性都很優秀的二進制數據傳輸格式,可以用于諸如網絡傳輸、配置文件、數據存儲等諸多領域。
.proto
類似于.json和.xml,ProtoBuf有自己的文件格式.proto文件格式。
也有自己的語法,具體可以搜索.proto語法。
示例源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11583786
實現
新建.proto文件
這里使用的是EditPlus新建txt文件,按照其語法要求編寫如下request.proto文件。
package ProtoBufTest;message Request {required int32 id = 1;required string password = 2; }注:
package ProtoBufTest 要與項目的namespace相對應。
message 后面就是要生成的類名。
required表示必須的,int32對應int類型,string對應string類型。
=1是固定的語法格式,記得往后遞增。
最終文件如下
?
.ptoto文件生成類(.cs文件)
生成工具ProtoGen下載:
https://download.csdn.net/download/badao_liumang_qizhi/11583806
將上面新建的文件放在與protogen.exe同目錄下
?
然后在此處打開命令行(Windows下是按住shift,在當前目錄右擊選擇在此處打開命令行)。
輸入命令:
protogen.exe?? -i:request.proto??? -o:Request.cs?
注:
-i后面跟的是上面新建的proto文件
-o后面跟的是要生成的.cs文件
運行后會在同目錄下生成Request.cs
?
生成的文件內容
//------------------------------------------------------------------------------ // <auto-generated> //???? This code was generated by a tool. // //???? Changes to this file may cause incorrect behavior and will be lost if //???? the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------// Generated from: request.proto namespace ProtoBufTest {[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Request")]public partial class Request : global::ProtoBuf.IExtensible{public Request() {}private int _id;[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]public int id{get { return _id; }set { _id = value; }}private string _password;[global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"password", DataFormat = global::ProtoBuf.DataFormat.Default)]public string password{get { return _password; }set { _password = value; }}private global::ProtoBuf.IExtension extensionObject;global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing){ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }}}項目中引進
新建窗體項目,然后將上面生成的Request.cs文件復制到項目下。
此時打開cs文件會報錯,此時需要引進dll(動態鏈接庫文件)--protobuf-net.dll。
protobuf-net.dll下載:
https://download.csdn.net/download/badao_liumang_qizhi/11583772
添加引用
打開資源管理器--右擊引用--添加
?
選擇瀏覽--右下角瀏覽--確定
?
實現ProtoBuf序列化
打開窗體設計器,拖拽一個按鈕Button,然后雙擊按鈕進入其點擊事件。
?private void button1_Click(object sender, EventArgs e){//序列化操作Request request = new Request();request.id = 1;request.password = "123";//計時器計時Stopwatch sw = new Stopwatch();//啟動計時器sw.Start();//執行序列化MemoryStream ms = new MemoryStream();Serializer.Serialize<Request>(ms, request);byte[] data = ms.ToArray();//停止計時器sw.Stop();//輸出計時時間Console.WriteLine("使用ProtoBuf序列化:" + sw.Elapsed);????}注:
在上面調用Serializer時引入的是ProtoBuf自帶的。
?
對比二進制序列化
右擊項目-添加-類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ProtoBufTest {[Serializable]class Person{public Person(int id ,string password) {this.Id = id;this.Password = password;}private int id;public int Id{get { return id; }set { id = value; }}private string password;public string Password{get { return password; }set { password = value; }}} }注:記得要添加可序列化的標識[Serializable]
在上面的窗體中在拖拽一個Button,雙擊進入其點擊事件。
private void button2_Click(object sender, EventArgs e){Person per = new Person(18,"張三");//計時器計時Stopwatch sw = new Stopwatch();sw.Start();FileStream fs = new FileStream(filePath, FileMode.Create);BinaryFormatter bf = new BinaryFormatter();bf.Serialize(fs, per);fs.Close();sw.Stop();Console.WriteLine("二進制序列化:" + sw.Elapsed);}對比效果
運行項目,先點擊二進制序列化按鈕,再點擊ProtoBuf序列化按鈕。
?
?
?
總結
以上是生活随笔為你收集整理的C#中使用ProtoBuf提高序列化速度对比二进制序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中提示:System.Runtime
- 下一篇: C#中将list进行二进制序列化并保存数