C#中使用ProtoBuf将list进行序列化并保存到文件
生活随笔
收集整理的這篇文章主要介紹了
C#中使用ProtoBuf将list进行序列化并保存到文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
C#中使用ProtoBuf提高序列化速度對比二進制序列化:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99850052
實現(xiàn)
參考上面那篇博客,進而將序列化后的數(shù)據(jù)寫入文件。
新建Winforn程序,然后拖拽兩個Button。
第一個Button用來構建一個1000長度的list對象。
先聲明一個全局的list變量。
List<Request> requestList = new List<Request>();然后在第一個按鈕點擊事件中
??
private void button4_Click(object sender, EventArgs e){DateTime begin = DateTime.Now;Console.WriteLine("protobuf初始化數(shù)據(jù)開始" + begin.ToString("yyyy-MM-dd HH:mm:ss"));for (int i = 1; i <= 1000; i++){requestList.Add(new Request() { id = i,password = "密碼"+i});}DateTime end = DateTime.Now;TimeSpan ts = end - begin;Console.WriteLine("protobuf初始化數(shù)據(jù)結束" + end.ToString("yyyy-MM-dd HH:mm:ss"));Console.WriteLine("共花費" + ts.TotalSeconds);}注:
這里的類文件是根據(jù).prpto文件生成的,所以其初始化賦值方式注意要如上使用。
然后在第二個按鈕的點擊事件中:
?
private void button6_Click(object sender, EventArgs e){DateTime begin = DateTime.Now;Console.WriteLine("ProtoBuf保存數(shù)據(jù)開始" + begin.ToString("yyyy-MM-dd HH:mm:ss"));using (System.IO.FileStream fs = new System.IO.FileStream(@"E:\testdata1\Record2.data", System.IO.FileMode.Create, System.IO.FileAccess.Write)){ProtoBuf.Serializer.Serialize(fs, this.requestList);}DateTime end = DateTime.Now;TimeSpan ts = end - begin;Console.WriteLine("ProtoBuf保存數(shù)據(jù)結束" + end.ToString("yyyy-MM-dd HH:mm:ss"));Console.WriteLine("花費時間" + ts.TotalSeconds);}?
總結
以上是生活随笔為你收集整理的C#中使用ProtoBuf将list进行序列化并保存到文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中将list进行二进制序列化并保存数
- 下一篇: C# Winform程序中使用TeeC