1

I have to parse XML document before deserializing. Does it make sense to reuse parsed result (by creating reader) or there is no performance difference?

var root = XDocument.Parse(message).Root;
var type = mes3.Name.LocalName;
if (type == typeOf(SomeType))
{
    var reader = root.CreateReader();
    var serializer = new XmlSerializer(typeof(SomeType));
    var someType = serializer.Deserialize(reader);
}
1
  • 2
    The only way to answer that properly is to time it. Commented Apr 2, 2014 at 11:24

3 Answers 3

2

It is preferable to reuse the parsed result by creating a reader instead of deserializing using directly the string message as the string would not be parsed again during deserilization.

If we use a quick and dirty demo program:

using System;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;

namespace Sample_04_03_2014_01
{
    public class Sample
    {
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample s = new Sample();
            s.Name = "Hello";
            var serializer = new XmlSerializer(typeof(Sample));
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                serializer.Serialize(sw, s);
            }
            string serialized = sb.ToString();
            Console.WriteLine(serialized);

            var root = XDocument.Parse(serialized).Root;
            Sample someType1 = null;
            Stopwatch stopWatch1 = new Stopwatch();
            stopWatch1.Start();
            for (int i = 0; i < 100000; i++)
            {
                var serializer1 = new XmlSerializer(typeof(Sample));
                using (var reader = root.CreateReader())
                {
                    someType1 = (Sample)serializer1.Deserialize(reader);
                }
            }
            stopWatch1.Stop();
            Console.WriteLine(someType1.Name);
            Console.WriteLine(stopWatch1.Elapsed);

            Sample someType2 = null;
            Stopwatch stopWatch2 = new Stopwatch();
            stopWatch2.Start();
            for (int i = 0; i < 100000; i++)
            {
                var serializer2 = new XmlSerializer(typeof(Sample));
                using (TextReader reader = new StringReader(serialized))
                {
                    someType2 = (Sample)serializer2.Deserialize(reader);
                }
            }
            stopWatch2.Stop();
            Console.WriteLine(someType2.Name);
            Console.WriteLine(stopWatch2.Elapsed);
        }
    }
}

then we obtain ~ 30% performance gain for the first method (CreateReader).

Hello 00:00:00.8825465 Hello 00:00:01.2636450

Sign up to request clarification or add additional context in comments.

Comments

0

your code has same performance with:

using (TextReader reader = new StringReader(message))
{
  result = serializer.Deserialize(reader);
}

by you have extra objects.....

Comments

0

I use this

using (FileStream xmlFile = new FileStream(filePath, FileMode.Open))
{
       resultclass = (resultclasstype)serializer.Deserialize(xmlFile);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.