Performance: LINQ to XML vs XmlDocument vs XmlReader

| | Comments (4) | TrackBacks (0)

I recently had a project where I needed to ingest large XML documents using C# so I was curious which XML reader technology would be the fastest. So I coded up a quick benchmark that would compare LINQ to XML, XmlDocument.Load, and XmlReader against each other.

The Test Data

I generated a very simple XML file before each run of a test. The id's were random and the number of "child" nodes varied based on the run. The following is an example of the test data I used.
<root>
  <child id='123'/>
  <child id='234'/>
  ...
</root>

The Test

As I said before I wanted to compare LINQ to XML, XmlDocument.Load, and XmlReader against each other. I ran each of these technologies using 1, 10, 100, 1000, 10,000, 100,000 "child" nodes. I also ran each against a XML document using UTF-8, ASCII, and UTF-32 encodings. Each iteration was run 100 times to reduce anomalies. In each of the tests I call the method "ProcessId" which simulates the processing of the "id" attribute.

XmlDocument.Load

I thought the code for XmlDocument.Load was the cleanest and easiest to understand, although I must admit I like XPath. XmlDocument does have some security concerns but that's another post. Here is the code I used to load and search the document:
private static void XmlDocumentReader(string fileName) {
    XmlDocument doc = new XmlDocument();
    doc.Load(fileName);
    XmlNodeList nodes = doc.SelectNodes("//child");
    if (nodes == null) {
        throw new ApplicationException("invalid data");
    }
    foreach (XmlNode node in nodes) {
        string id = node.Attributes["id"].Value;
        ProcessId(id);
    }
}

LINQ to XML

LINQ to XML was also very easy to read and understand code. I did find that even though LINQ to XML is supposed to use XmlReaders under the covers calling XDocument.Load does read the whole document into memory before returning. So if you are looking for data at the top of middle of a very large document this could be a concern. Here is the code I used to load and search the document:
private static void XDocumentReader(string fileName) {
    XDocument doc = XDocument.Load(fileName);
    if (doc == null | doc.Root == null) {
        throw new ApplicationException("invalid data");
    }
    foreach (XElement child in doc.Root.Elements("child")) {
        XAttribute attr = child.Attribute("id");
        if (attr == null) {
            throw new ApplicationException("invalid data");
        }
        string id = attr.Value;
        ProcessId(id);
    }
}

XmlReader

XmlReader, specifically XmlTextReader was the hardest to write and understand. With it's quirks of being a forward only reader you need to take what you need while you have it because you can't rewind.
private static void XmlReaderReader(string fileName) {
    using (XmlReader reader = new XmlTextReader(fileName)) {
        while (reader.Read()) {
            if (reader.NodeType == XmlNodeType.Element) {
                if (reader.Name == "child") {
                    reader.MoveToAttribute("id");
                    string id = reader.Value;
                    ProcessId(id);
                }
            }
        }
    }
}

The Results

The following results are in milliseconds for each run. I took the total time to run and divided it by 100.

UTF8Encoding

1101001,00010,000100,000
XmlDocument0.15678000.17134500.38886201.981648022.8049260459.8570340
XmlReader0.14674600.14395800.23005000.85344007.577164076.8635690
LINQ to XML0.14995300.15006400.27787201.461673015.7719020208.9360300

ASCIIEncoding

1101001,00010,000100,000
XmlDocument0.16593500.19220800.34331401.984633022.5484690482.8699720
XmlReader0.13768400.14537300.21998100.87682607.918738077.7760560
LINQ to XML0.13459000.15733400.28484201.488993015.1504500214.9338990

UTF32Encoding

1101001,00010,000100,000
XmlDocument0.16723700.17997800.41562502.718837030.6423960543.4604540
XmlReader0.13868200.15038700.28674001.498107014.4428430152.7660780
LINQ to XML0.13170600.18666100.53859402.363129021.4566290274.3280280

Conclusion

XmlReader beats LINQ to XML in almost every run except for very small XML documents. What's interesting is how the numbers scale between the encodings. XmlReader is over twice as slow when reading UTF-32 documents verse UTF-8 or ASCII encoded XML, yet LINQ to XML and XmlDocument slowed down by a much smaller amount. If you need speed when reading XML documents stick with XmlReader. If you need readability and maintainability of your code go with LINQ to SQL or XmlDocument.

4 Comments

Andrew said:

Very interesting article.
Regarding your comment about utf32 encoding.
The percentage change of the numbers don't make sense, but if you look at the absolute change, the numbers are much closer.
For instance. For 100,000 nodes, comparing ascii to utf32.
XmlDocument -> 61s difference
XmlReader -> 75s difference
LINQ to XML -> 60s difference

I'm guessing that XmlReader spends most of it's time reading the document(which is 4 times larger), as opposed to processing the document.

Anders said:

You can make the LINQ to XML code substantially simpler. Two lines should do it:

var ids = XElement.Load(fileName).Elements("child").Attributes("id");
foreach (XAttribute a in ids) ProcessId((string)id);

It is not surprising the XmlReader code is faster since both the XML DOM (XmlDocument) and LINQ to XML use XmlReader to do their reading.

Anders

james osburn said:

what is the fast way to generate an xml document?
from say web service?
regards
james

thorn said:

Didn't you forget about XPathDocument?

Leave a comment


Type the characters you see in the picture above.

0 TrackBacks

Listed below are links to blogs that reference this entry: Performance: LINQ to XML vs XmlDocument vs XmlReader.

TrackBack URL for this entry: http://www.nearinfinity.com/mt/mt-tb.cgi/490