Behzad
Behzad

Reputation: 877

XML Serialize and deserialize performance comparison

This question asked in different websites but i could not find any useful answer, and still im having some performance issues. I have two serializer method in my common layer application

    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();
    } 

and two deserializer method

    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;
    }

I have run different load tests in both of serializer methods, and all of them shown that Serializer<T>(T o) works slower than Serializer(object o), which in my opinion must be reverse since typeof() is faster and the type is known unlike object. I would like to know about your opinions first ?
and second, the serializer and deserializer methods used in another method named

 public static TResponse  SendRequest <TRequest,TResponse>(TRequest rq, Uri requestUri) 

which is responsible to send the request to web server and get the response back, is there anyway to make it more efficient ?

Upvotes: 2

Views: 1397

Answers (1)

EngineerSpock
EngineerSpock

Reputation: 2675

I wrote the following code and I didn't notice any significant difference. Though, serializing through generics is slightly faster. Here is the code:

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();
        }
    }

Maybe it would be better to share with us a class you are trying to serialize/deserialize and share the code by which you made your estimations of executing time of serializing methods.

Upvotes: 1

Related Questions