C#中使用ProtoBuf提高序列化速度对比二进制序列化
場景
ProtoBuf
protocolbuffer是google 的一種數(shù)據(jù)交換的格式,它獨(dú)立于語言,獨(dú)立于平臺。
google 提供了多種語言的實(shí)現(xiàn):java、c#、c++、go 和 python,每一種實(shí)現(xiàn)都包含了相應(yīng)語言的編譯器以及庫文件。
由于它是一種二進(jìn)制的格式,比使用xml 進(jìn)行數(shù)據(jù)交換快許多。可以把它用于分布式應(yīng)用之間的數(shù)據(jù)通信或者異構(gòu)環(huán)境下的數(shù)據(jù)交換。
作為一種效率和兼容性都很優(yōu)秀的二進(jìn)制數(shù)據(jù)傳輸格式,可以用于諸如網(wǎng)絡(luò)傳輸、配置文件、數(shù)據(jù)存儲等諸多領(lǐng)域。
.proto
類似于.json和.xml,ProtoBuf有自己的文件格式.proto文件格式。
也有自己的語法,具體可以搜索.proto語法。
示例源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11583786
實(shí)現(xiàn)
新建.proto文件
這里使用的是EditPlus新建txt文件,按照其語法要求編寫如下request.proto文件。
package ProtoBufTest;message Request {required int32 id = 1;required string password = 2; }注:
package ProtoBufTest 要與項(xiàng)目的namespace相對應(yīng)。
message 后面就是要生成的類名。
required表示必須的,int32對應(yīng)int類型,string對應(yīng)string類型。
=1是固定的語法格式,記得往后遞增。
最終文件如下
?
.ptoto文件生成類(.cs文件)
生成工具ProtoGen下載:
https://download.csdn.net/download/badao_liumang_qizhi/11583806
將上面新建的文件放在與protogen.exe同目錄下
?
然后在此處打開命令行(Windows下是按住shift,在當(dāng)前目錄右擊選擇在此處打開命令行)。
輸入命令:
protogen.exe?? -i:request.proto??? -o:Request.cs?
注:
-i后面跟的是上面新建的proto文件
-o后面跟的是要生成的.cs文件
運(yùn)行后會在同目錄下生成Request.cs
?
生成的文件內(nèi)容
//------------------------------------------------------------------------------ // <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); }}}項(xiàng)目中引進(jìn)
新建窗體項(xiàng)目,然后將上面生成的Request.cs文件復(fù)制到項(xiàng)目下。
此時(shí)打開cs文件會報(bào)錯(cuò),此時(shí)需要引進(jìn)dll(動態(tài)鏈接庫文件)--protobuf-net.dll。
protobuf-net.dll下載:
https://download.csdn.net/download/badao_liumang_qizhi/11583772
添加引用
打開資源管理器--右擊引用--添加
?
選擇瀏覽--右下角瀏覽--確定
?
實(shí)現(xiàn)ProtoBuf序列化
打開窗體設(shè)計(jì)器,拖拽一個(gè)按鈕Button,然后雙擊按鈕進(jìn)入其點(diǎn)擊事件。
?private void button1_Click(object sender, EventArgs e){//序列化操作Request request = new Request();request.id = 1;request.password = "123";//計(jì)時(shí)器計(jì)時(shí)Stopwatch sw = new Stopwatch();//啟動計(jì)時(shí)器sw.Start();//執(zhí)行序列化MemoryStream ms = new MemoryStream();Serializer.Serialize<Request>(ms, request);byte[] data = ms.ToArray();//停止計(jì)時(shí)器sw.Stop();//輸出計(jì)時(shí)時(shí)間Console.WriteLine("使用ProtoBuf序列化:" + sw.Elapsed);????}注:
在上面調(diào)用Serializer時(shí)引入的是ProtoBuf自帶的。
?
對比二進(jìn)制序列化
右擊項(xiàng)目-添加-類
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; }}} }注:記得要添加可序列化的標(biāo)識[Serializable]
在上面的窗體中在拖拽一個(gè)Button,雙擊進(jìn)入其點(diǎn)擊事件。
private void button2_Click(object sender, EventArgs e){Person per = new Person(18,"張三");//計(jì)時(shí)器計(jì)時(shí)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("二進(jìn)制序列化:" + sw.Elapsed);}對比效果
運(yùn)行項(xiàng)目,先點(diǎn)擊二進(jìn)制序列化按鈕,再點(diǎn)擊ProtoBuf序列化按鈕。
?
?
?
總結(jié)
以上是生活随笔為你收集整理的C#中使用ProtoBuf提高序列化速度对比二进制序列化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中提示:System.Runtime
- 下一篇: C#中将list进行二进制序列化并保存数