首页 新闻 会员 周边 捐助

三种序列化方法比较

0
[已解决问题] 解决于 2015-01-26 14:14

      以下代码比较了二进制序列化、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;}
    }  
}
Freedom0619的主页 Freedom0619 | 初学一级 | 园豆:10
提问于:2014-11-25 21:07
< >
分享
最佳答案
1

你是指比较运行时间,或者说比较性能的代码?

如果是的话,赵姐夫写过一个工具类,还是开源的,你可以在园里面找到的。

奖励园豆:5
爱编程的大叔 | 高人七级 |园豆:30844 | 2014-11-25 21:23

@Freedom0619: 帮你找出来了。

一个简单的性能计数器:CodeTimer 

爱编程的大叔 | 园豆:30844 (高人七级) | 2014-11-25 22:00
其他回答(1)
2

没必要,你只需要写好方法,然后用 VS 创建测试项目即可。

Launcher | 园豆:45050 (高人七级) | 2014-11-26 09:11

@Freedom0619: 写的难看是因为你把三种序列化方法写在了一起,并且夹杂了测试代码。正确的做法是这样:

[TestMethod]

void ObjectToFileBinarySerialize()

{

BinaryFormatter bf = new BinaryFormatter();
                Stream BinaryStream = new FileStream("e:/person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
               
                bf.Serialize(BinaryStream, list);
                BinaryStream.Close();

}

支持(1) 反对(0) Launcher | 园豆:45050 (高人七级) | 2014-11-26 09:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册