首页 > 代码库 > Protobuf-net实现序列化

Protobuf-net实现序列化

引入Protobuf-net的dll,

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Xml.Serialization;using System.Diagnostics;namespace ProtoBuf{    class Program    {        static int testCount = 100;        static void Main(string[] args)        {            var list = InitData();            TestProtoBufSerialize(list);            //TestDeProtoBufSerialize(list);            //TestBinarySerialize(list);            //TestDeBinarySerialize(list);            Console.Read();        }        private static List<Person> InitData()        {            List<Person> list = new List<Person>();            for (var i = 0; i < 1024*100; i++)            {                var person = new Person                {                    Sno = i,                    Name = "Name" + i,                    Age = 20 + i,                    HomeTown = "HomeTown" + i,                    Shool = "Shool" + i,                    Country = "Country" + i,                    Language = "Language" + i,                    Professional = "professional" + i,                    Study = "study" + i,                    FatherName = "fatherName" + i,                    MotherName = "motherName" + i                };                list.Add(person);            }            return list;        }        static void TestProtoBufSerialize(List<Person> list)        {            Stopwatch NPS = new Stopwatch();            NPS.Start();            Stream stream = new MemoryStream();            for (int i = 0; i < testCount; i++)            {                ProtobufSerializa2(list, stream);            }            NPS.Stop();            Console.WriteLine("一次内存流" + NPS.Elapsed.TotalMilliseconds);            Stopwatch PS = new Stopwatch();            PS.Start();            for (int i = 0; i < testCount; i++)            {                ProtobufSerializa(list);            }            PS.Stop();            Console.WriteLine("原方法" + PS.Elapsed.TotalMilliseconds);            Stopwatch NPSs = new Stopwatch();            NPSs.Start();            for (int i = 0; i < testCount; i++)            {                ProtobufSerializa3(list);            }            NPSs.Stop();            Console.WriteLine("改进方法" + NPSs.Elapsed.TotalMilliseconds);        }        //static void TestDeProtoBufSerialize(List<Person> list)        //{        //    Stopwatch NePS = new Stopwatch();        //    NePS.Start();        //    for (int i = 0; i < testCount; i++)        //    {        //        deProtobufSerializa3(list);        //    }        //    NePS.Stop();        //    Console.WriteLine("反序列化方法3用时" + NePS.Elapsed.TotalMilliseconds);               //    Stopwatch NdePS = new Stopwatch();        //    NdePS.Start();        //    for (int i = 0; i < testCount; i++)        //    {        //        deProtobufSerializa2(list);        //    }        //    NdePS.Stop();        //    Console.WriteLine("反序列化方法2用时" + NdePS.Elapsed.TotalMilliseconds);        //    Stopwatch dePS = new Stopwatch();        //    dePS.Start();        //    for (int i = 0; i < testCount; i++)        //    {        //        deProtobufSerializa(list);        //    }        //    dePS.Stop();        //    Console.WriteLine("反序列化方法1用时" + dePS.Elapsed.TotalMilliseconds);        //}        //static void TestBinarySerialize(List<Person> list)        //{        //    Stopwatch bsTime = new Stopwatch();        //    bsTime.Start();        //    for (int i = 0; i < testCount; i++)        //    {        //        BinarySerializa(list);        //    }        //    bsTime.Stop();        //    Console.WriteLine("二进制序列化用时" + bsTime.Elapsed.TotalMilliseconds);        //}        //static void TestDeBinarySerialize(List<Person> list)        //{        //    Stopwatch debsTime = new Stopwatch();        //    debsTime.Start();        //    for (int i = 0; i < testCount; i++)        //    {        //        deBinarySerializa(list);        //    }        //    debsTime.Stop();        //    Console.WriteLine("二进制序列化用时" + debsTime.Elapsed.TotalMilliseconds);        //}               //原方法        static void ProtobufSerializa(List<Person> list)        {            Stream ProtoBufStream = new MemoryStream();            ProtoBuf.Serializer.Serialize(ProtoBufStream, list);            //Console.WriteLine(ProtoBufStream.Position);            //Console.WriteLine(ProtoBufStream.Length);            ProtoBufStream.Close();            ProtoBufStream.Dispose();        }        //仅计算一次内存流创建        static void ProtobufSerializa2(List<Person> list, Stream ProtoBufStream)        {            ProtoBuf.Serializer.Serialize(ProtoBufStream, list);        }        //计算是否有内存流再考虑是否创建        static void ProtobufSerializa3(List<Person> list)        {            var stream = MemoryStreamPool.AcquireMemoryStream();            ProtoBuf.Serializer.Serialize(stream, list);            MemoryStreamPool.Return(stream);        }        //static void BinarySerializa(List<Person> list)        //{        //    BinaryFormatter bf = new BinaryFormatter();        //    Stream BinaryStream = new FileStream("e:/person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);        //    bf.Serialize(BinaryStream, list);        //    BinaryStream.Close();        //}        //static void deProtobufSerializa(List<Person> list)        //{        //    Stream deProtoBufStream = new MemoryStream();        //    Person newPerson = ProtoBuf.Serializer.Deserialize<Person>(deProtoBufStream);        //    deProtoBufStream.Close();        //}        //static void deProtobufSerializa2(List<Person> list)        //{        //    var stream = MemoryStreamPool.AcquireMemoryStream();        //    Person newPerson = ProtoBuf.Serializer.Deserialize<Person>(stream);        //    //MemoryStreamPool.Return(stream);        //}        //static void deProtobufSerializa3(List<Person> list)        //{        //    var stream = MemoryStreamPool.AcquireMemoryStream();        //    Person newPerson = ProtoBuf.Serializer.Deserialize<Person>(stream);        //    //MemoryStreamPool.Return(stream);        //}        //static void deBinarySerializa(List<Person> list)        //{        //    BinaryFormatter bf = new BinaryFormatter();        //    Stream deBinaryStream = new FileStream("e:/person.txt", FileMode.Open, FileAccess.Read, FileShare.Read);        //    object obj = bf.Deserialize(deBinaryStream);        //    deBinaryStream.Close();        //}        //static void Serializa(List<Person> list, int method)        //{        //    if (method == 1)        //    {        //        BinarySerializa(list);        //    }        //    else        //    {        //        throw new NotSupportedException();        //    }        //}        //static void TestSerializa(List<Person> list, int method)        //{        //    Stopwatch stopwatch = Stopwatch.StartNew();        //    for (int i = 0; i < testCount; i++)        //    {        //        Serializa(list, method);        //    }        //    stopwatch.Stop();        //    Console.WriteLine("method:{0} time cost {1}ms", method, stopwatch.ElapsedMilliseconds);        //}        //static void Test(List<Person> list)        //{        //    int count = 100;        //    double SumTs1 = 0, SumTs2 = 0, SumTs3 = 0, SumTs4 = 0, SumTs5 = 0, SumTs6 = 0;        //    do        //    {        //        //binary序列化开始        //        DateTime binaryTime1 = System.DateTime.Now;        //        BinaryFormatter bf = new BinaryFormatter();        //        Stream BinaryStream = new FileStream("e:/person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);        //        //Stream BinaryStream = new MemoryStream();        //        bf.Serialize(BinaryStream, list);        //        //byte[] b = new byte[BinaryStream.Length];        //        //BinaryStream.Position = 0;        //        //BinaryStream.Read(b, 0, b.Length);        //        BinaryStream.Close();        //        DateTime binaryTime2 = System.DateTime.Now;        //        //binary反序列化开始        //        DateTime debinaryTime1 = System.DateTime.Now;        //        Stream deBinaryStream = new FileStream("e:/person.txt", FileMode.Open, FileAccess.Read, FileShare.Read);        //        //Stream deBinaryStream = new MemoryStream();        //        object obj = bf.Deserialize(deBinaryStream);        //        deBinaryStream.Close();        //        DateTime debinaryTime2 = System.DateTime.Now;        //        //Xml序列化开始        //        DateTime XmlTime1 = System.DateTime.Now;        //        XmlSerializer xs = new XmlSerializer(typeof(List<Person>));        //        Stream XmlStream = new FileStream("e:/2person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);        //        //Stream XmlStream = new MemoryStream();        //        xs.Serialize(XmlStream, list);        //        XmlStream.Close();        //        DateTime XmlTime2 = System.DateTime.Now;        //        //Xml反序列化开始        //        DateTime deXmlTime1 = System.DateTime.Now;        //        XmlSerializer dexs = new XmlSerializer(typeof(List<Person>));        //        Stream deXmlStream = new FileStream("e:/2person.txt", FileMode.Open, FileAccess.Read, FileShare.Read);        //        //Stream deXmlStream = new MemoryStream();        //        List<Person> list2 = (List<Person>)dexs.Deserialize(deXmlStream);        //        deXmlStream.Close();        //        DateTime deXmlTime2 = System.DateTime.Now;        //        //ProtoBuf序列化开始        //        DateTime ProtoBufTime1 = System.DateTime.Now;        //        Stream ProtoBufStream = new MemoryStream();        //        ProtoBuf.Serializer.Serialize(ProtoBufStream, list);        //        ProtoBufStream.Close();        //        DateTime ProtoBufTime2 = System.DateTime.Now;        //        //ProtoBuf反序列化开始        //        DateTime deProtoBufTime1 = System.DateTime.Now;        //        Stream deProtoBufStream = new MemoryStream();        //        Person newPerson = ProtoBuf.Serializer.Deserialize<Person>(deProtoBufStream);        //        deProtoBufStream.Close();        //        DateTime deProtoBufTime2 = System.DateTime.Now;        //        //计算时间和运行次数        //        SumTs1 += binaryTime2.Subtract(binaryTime1).TotalMilliseconds;        //        SumTs2 += debinaryTime2.Subtract(debinaryTime1).TotalMilliseconds;        //        SumTs3 += XmlTime2.Subtract(XmlTime1).TotalMilliseconds;        //        SumTs4 += deXmlTime2.Subtract(deXmlTime1).TotalMilliseconds;        //        SumTs5 += ProtoBufTime2.Subtract(ProtoBufTime1).TotalMilliseconds;        //        SumTs6 += deProtoBufTime2.Subtract(deProtoBufTime1).TotalMilliseconds;        //        count--;        //    } while (count > 0);        //    Console.WriteLine("*********************************序列化比较结果************************************");        //    Console.WriteLine("***            binary序列化        Xml序列化         ProtoBuf序列化        ");        //    Console.WriteLine("***序列化      " + SumTs1 / 100 + "     " + SumTs3 / 100 + "          " + SumTs5 / 100);        //    Console.WriteLine("***反序列化    " + SumTs2 / 100 + "             " + SumTs4 / 100 + "         " + SumTs6 / 100);        //    Console.WriteLine("**********************************************************************************");        //}    }    public class MemoryStreamPool    {        //Stopwatch s = new Stopwatch();        static MemoryStream instance;        public static MemoryStream AcquireMemoryStream()        {            if (instance == null)            {                instance = new MemoryStream();            }            return instance;        }        public static void Return(MemoryStream stream)        {            instance.Flush();        }    }    [ProtoBuf.ProtoContract]    [Serializable]    public class Person    {        public int Sno { get; set; }        public string Name { get; set; }        public int Age { get; set; }        public string HomeTown { get; set; }        public string Shool { get; set; }        public string Country { get; set; }        public string Language { get; set; }        public string Professional { get; set; }        public string Study { get; set; }        public string FatherName { get; set; }        public string MotherName { get; set; }    }}

 

Protobuf-net实现序列化