{"id":2524,"date":"2007-08-22T01:01:58","date_gmt":"2007-08-22T01:01:58","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/performance-caching-vs-reading-from-an-in-memory-xml-document\/"},"modified":"2016-07-28T10:49:08","modified_gmt":"2016-07-28T10:49:08","slug":"performance-caching-vs-reading-from-an-in-memory-xml-document","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/performance-caching-vs-reading-from-an-in-memory-xml-document\/","title":{"rendered":"Performance: Caching vs. Reading from an In-Memory XML Document"},"content":{"rendered":"<p>I&#8217;ve been working with a number of XML documents recently.&#160; Basically I&#8217;m trying to expose configuration values stored in XML configuration files in an object-oriented structure.&#160; I&#8217;m using an in-memory XML document and just referencing values as needed from that document.&#160; The XML reading does not need to be super-high performance because it does not occur very often, but it got me to thinking&#8230; what if it did?<\/p>\n<p>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.&#160; <\/p>\n<p>The results (at least on my machine) are as follows:<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">&#160;<\/td>\n<td valign=\"top\">\n<p>Iterations \/ Millisecond<\/p>\n<\/td>\n<td valign=\"top\">Performance Difference<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">XML (attribute value)<\/td>\n<td valign=\"top\">\n<p>8,648<\/p>\n<\/td>\n<td valign=\"top\">\n<p>90% slower<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">XML (inner text value)<\/td>\n<td valign=\"top\">\n<p>21,694<\/p>\n<\/td>\n<td valign=\"top\">\n<p>76% slower<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Cached Property<\/td>\n<td valign=\"top\">\n<p>90,140<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I knew caching would beat out the other two methods since it&#8217;s very fast to access a direct variable, but I was surprised that there was such a difference.&#160; 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.&#160; Live and learn.&#160; Of course, reading from an in-memory XML document is still pretty dang fast, it&#8217;s just that reading from a cached value is faster.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working with a number of XML documents recently.&#160; Basically I&#8217;m trying to expose configuration values stored in XML configuration files in an object-oriented structure.&#160; I&#8217;m using an in-memory XML document and just referencing values as needed from that document.&#160; The XML reading does not need to be super-high performance because it does not&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2524","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2524"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2524\/revisions"}],"predecessor-version":[{"id":41545,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2524\/revisions\/41545"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2524"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}