• 三种序列化方式性能比较


    一下代码比较了二进制序列化、xml序列化、Protobuf序列化的运行时间,可是代码显得十分冗余,是否有大神可以指点一二,万分感谢

    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 void Main(string[] args)
            {
                List<Person> list = new List<Person>();
                int count = 100;
                double SumTs1 = 0, SumTs2 = 0,SumTs3=0,SumTs4=0,SumTs5=0,SumTs6=0;
                for (var i=0;i<1000;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);
                }
                do
                {
                    //binary序列化开始
                    DateTime binaryTime1 = System.DateTime.Now;
                    BinaryFormatter bf = new BinaryFormatter();
                    Stream BinaryStream = new FileStream("e:/person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
                    //Stream BinaryStream = new MemoryStream();
                    bf.Serialize(BinaryStream, list);
                    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(BinaryStream);
                    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);
                    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("**********************************************************************************");
            }
        }
    
        [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;}
        }   
    }
    
  • 相关阅读:
    Docker(五):Dockerfile
    Docker(四):Docker常用命令
    CentOS搭建NodeJs服务器—Mongodb安装
    CentOS7 yum 安装git
    Linux kswapd0 进程CPU占用过高
    数据库-最开始的数据库建立
    小程序-自定义组件数据传递
    jQuery-很上头的语言基础整理
    小程序-自定义组件理解
    vue中图片预览(v-viewer库使用)
  • 原文地址:https://www.cnblogs.com/Freedom0619/p/4121630.html
Copyright © 2020-2023  润新知