XML 序列化与反序列化性能对比

XML Serialize and deserialize performance comparison

这个问题在不同的网站上提出过,但我找不到任何有用的答案,而且我仍然遇到一些性能问题。 我的公共层应用程序中有两个序列化程序方法

    public static string Serializer(object o)
    {
        var x = new XmlSerializer(o.GetType());
        var writer = new StringWriter();
        var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
        var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
        x.Serialize(xmlWriter, o, emptyNs);
        return writer.ToString();
    }

    public static string Serializer<T>(T o)
    {
        var x = new XmlSerializer(typeof(T));
        var writer = new StringWriter();
        var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
        x.Serialize(xmlWriter, o, new XmlSerializerNamespaces( new[] { XmlQualifiedName.Empty } ));
        return writer.ToString();
    } 

和两种解串器方法

    public static T Deserializer<T>(string objectData)
    {
        var serializer = new XmlSerializer(typeof(T));
        T result;
        using (TextReader reader = new StringReader(objectData))
        {
            result =(T) serializer.Deserialize(reader);
        }
        return result;
    }

    public static object Deserializer(object o, string filename)
    {
        object retVal;
        var ser = new XmlSerializer(o.GetType());
        using (var reader = XmlReader.Create(filename))
        {
            retVal = ser.Deserialize(reader);
        }
        return retVal;
    }

我在两种序列化器方法中进行了 运行 不同的负载测试,所有测试都表明 Serializer<T>(T o)Serializer(object o) 工作得慢,我认为这必须是相反的,因为 typeof() 更快,并且类型已知,与对象不同。我想先了解一下您的意见?
其次,在另一个名为

的方法中使用的序列化器和反序列化器方法
 public static TResponse  SendRequest <TRequest,TResponse>(TRequest rq, Uri requestUri) 

负责将请求发送到 Web 服务器并取回响应,是否可以提高效率?

我编写了以下代码,但没有发现任何显着差异。不过,通过泛型进行序列化会稍微快一些。这是代码:

public class TestData {
        public string Name { get; set; }
        public string FullName { get; set; }
        public string Address { get; set; }
        public int PostalCode { get; set; }

        public TestData() {

        }
        public TestData(string name, string fullName, string address, int postalCode) {
            Name = name;
            FullName = fullName;
            Address = address;
            PostalCode = postalCode;
        }
    }

    public static class Program
    {

        public static string Serializer(object o)
        {
            var x = new XmlSerializer(o.GetType());
            var writer = new StringWriter();
            var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
            var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
            x.Serialize(xmlWriter, o, emptyNs);
            return writer.ToString();
        }

        public static string Serializer<T>(T o)
        {
            var x = new XmlSerializer(typeof(T));
            var writer = new StringWriter();
            var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
            x.Serialize(xmlWriter, o, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
            return writer.ToString();
        } 

        public static void Main(string[] args) {
            Random rand = new Random();
            const int numberOfCycles = 1000000;

            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < numberOfCycles; i++) {
                object data = new TestData("", "", "", rand.Next());
                Serializer(data);
            }
            watch.Stop();

            Console.WriteLine(string.Format("Through object:{0}", watch.ElapsedMilliseconds));

            watch.Restart();
            for (int i = 0; i < numberOfCycles; i++) {
                Serializer(new TestData("", "", "", rand.Next()));
            }            
            watch.Stop();
            Console.WriteLine(string.Format("Through generic:{0}", watch.ElapsedMilliseconds));

            Console.ReadLine();
        }
    }

也许最好与我们分享您正在尝试 serialize/deserialize 的 class 并分享您用来估算序列化方法执行时间的代码。