2

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 ?

Behzad
  • 857
  • 1
  • 8
  • 27

1 Answers1

1

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.

EngineerSpock
  • 2,575
  • 4
  • 35
  • 57
  • 2
    I tried your sample class code and it does shows the performance gain with the generic approach clearly. Please look into this C# fiddle https://dotnetfiddle.net/cSV0Ai – Karan Feb 26 '15 at 08:08
  • Yes, it is. In order to solve your problem you have to look for the problem somewhere else. My code proves that generic serialization works faster. If you consider my reply as answer, than mark it as answer, if not, than please extend the description of the situation you face, maybe more code, or create another topic. – EngineerSpock Feb 26 '15 at 10:08
  • Even I expect the generic serialization work faster, but my load test shows different result, anyway im convinced that generic serialization is having better performance. – Behzad Feb 26 '15 at 11:07
  • anyway do you have any suggestions for getting even better performance ? – Behzad Feb 26 '15 at 11:08
  • Your load tests performed under WCF or something? – EngineerSpock Feb 26 '15 at 11:26
  • nope, just simple test method which was serializing the given object. – Behzad Feb 27 '15 at 08:07
  • I will suggest the following: http://stackoverflow.com/questions/4143421/fastest-way-to-serialize-and-deserialize-net-object, and http://www.maxondev.com/serialization-performance-comparison-c-net-formats-frameworks-xmldatacontractserializer-xmlserializer-binaryformatter-json-newtonsoft-servicestack-text/ – EngineerSpock Feb 27 '15 at 10:11