Protobuf-net implementation of serialization

Source: Internet
Author: User

Introduction of Protobuf-net DLLs,

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 = + I, hometown = "Hometow           N "+ 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 ("One-time memory stream" + NPS.)            Elapsed.totalmilliseconds);            Stopwatch PS = new Stopwatch (); Ps.            Start ();            for (int i = 0; i < Testcount; i++) {Protobufserializa (list); }            Ps.            Stop (); Console.WriteLine ("Original Method" + PS.)            Elapsed.totalmilliseconds);            Stopwatch NPSS = new Stopwatch ();            Npss.start ();            for (int i = 0; i < Testcount; i++) {ProtobufSerializa3 (list);            } npss.stop ();        Console.WriteLine ("Improved Method" + NPSs.Elapsed.TotalMilliseconds); }//static void Testdeprotobufserialize (list<person> List)//{//Stopwatch neps = new STOPW        Atch ();        Neps.start ();        for (int i = 0; i < Testcount; i++)//{//DEPROTOBUFSERIALIZA3 (list);        }//Neps.stop ();               Console.WriteLine ("Deserialization Method 3 elapsed" + NePS.Elapsed.TotalMilliseconds);        Stopwatch ndeps = new Stopwatch ();        Ndeps.start ();        for (int i = 0; i < Testcount; i++)//{//DEPROTOBUFSERIALIZA2 (list); }//Ndeps.stoP ();        Console.WriteLine ("Deserialization Method 2 Elapsed" + NdePS.Elapsed.TotalMilliseconds);        Stopwatch DePS = new Stopwatch ();        Deps.start ();        for (int i = 0; i < Testcount; i++)//{//Deprotobufserializa (list);        }//Deps.stop ();        Console.WriteLine ("Deserialization Method 1 Elapsed" + dePS.Elapsed.TotalMilliseconds); }//static void Testbinaryserialize (list<person> List)//{//Stopwatch bstime = new STOPW        Atch ();        Bstime.start ();        for (int i = 0; i < Testcount; i++)//{//Binaryserializa (list);        }//Bstime.stop ();        Console.WriteLine ("Binary Serialization Time" + BsTime.Elapsed.TotalMilliseconds); }//static void Testdebinaryserialize (list<person> List)//{//Stopwatch debstime = new S        Topwatch ();        Debstime.start (); for (int i = 0; i < Testcount;        i++)//{//Debinaryserializa (list);        }//Debstime.stop ();        Console.WriteLine ("Binary Serialization Time" + DebsTime.Elapsed.TotalMilliseconds); }//original method static void Protobufserializa (List<person> List) {Stream Protobufs            Tream = new MemoryStream ();            ProtoBuf.Serializer.Serialize (Protobufstream, list);            Console.WriteLine (protobufstream.position);            Console.WriteLine (Protobufstream.length);            Protobufstream.close ();        Protobufstream.dispose ();            ///Compute only one time the memory stream creates a static void ProtobufSerializa2 (List<person> List, stream protobufstream) {        ProtoBuf.Serializer.Serialize (Protobufstream, list);  }//Calculates if there is a memory stream to consider whether to create a 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 Binaryfor        Matter ();        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 = NE        W MemoryStream ();        Person Newperson = protobuf.serializer.deserialize<person> (Deprotobufstream);        Deprotobufstream.close (); }//static void DeProtobufSerializa2 (list<person> List)//{//var stream = Memorystreampo Ol.        Acquirememorystream ();        Person Newperson = protobuf.serializer.deserialize<person> (stream);        Memorystreampool.return (stream); }//static VOID DeProtobufSerializa3 (list<person> List)//{//var stream = Memorystreampool.acquirememorystrea        M ();        Person Newperson = protobuf.serializer.deserialize<person> (stream);        Memorystreampool.return (stream); }//static void Debinaryserializa (list<person> List)//{//BinaryFormatter BF = new Binar        Yformatter ();        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 serialization start//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 deserialization start//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 serialization start//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 deserialization start//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 serialization Start//DateTime ProtoBufTime1 = System.DateTime.Now;        Stream Protobufstream = new MemoryStream ();        ProtoBuf.Serializer.Serialize (Protobufstream, list);        Protobufstream.close (); DateTime ProtoBufTime2 = SysteM.datetime.now;        PROTOBUF deserialization start//DateTime deProtoBufTime1 = System.DateTime.Now;        Stream Deprotobufstream = new MemoryStream ();        Person Newperson = protobuf.serializer.deserialize<person> (Deprotobufstream);        Deprotobufstream.close ();        DateTime deProtoBufTime2 = System.DateTime.Now; Calculate time and run times//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 ("*Serialization comparison result ************************************ ");        Console.WriteLine ("* * * binary serialization XML Serialization PROTOBUF serialization");        Console.WriteLine ("* * * serialization" + sumts1/100 + "" + sumts3/100 + "+ sumts5/100);        Console.WriteLine ("* * * deserialization" + 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) {INS            tance = 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 implementation of serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.