Performance: Caching vs. Reading from an In-Memory XML Document

I’ve been working with a number of XML documents recently.  Basically I’m trying to expose configuration values stored in XML configuration files in an object-oriented structure.  I’m using an in-memory XML document and just referencing values as needed from that document.  The XML reading does not need to be super-high performance because it does not occur very often, but it got me to thinking… what if it did?

So I ran some performance tests to check the difference between accessing a value from an XML document vs. storing the value in cache and accessing it through a standard get property. 

The results (at least on my machine) are as follows:

 

Iterations / Millisecond

Performance Difference
XML (attribute value)

8,648

90% slower

XML (inner text value)

21,694

76% slower

Cached Property

90,140

I knew caching would beat out the other two methods since it’s very fast to access a direct variable, but I was surprised that there was such a difference.  I figured there would be a bit of overhead for the inner text property, and a bit more for the attribute lookup, but I was thinking it would be in the range of 10-20% slower.  Live and learn.  Of course, reading from an in-memory XML document is still pretty dang fast, it’s just that reading from a cached value is faster.