odd performance question - xml parsing

G

Guest

Hi...

We've been doing some basic performance testing comparing asp, asp.net,
mono, and php. One of the basic tests is on simply parsing an xml document
and streaming the result back to the client.

I never would have thought of it, but someone in our group had heard that
using XmlDocument.Load (Server.MapPath (file));
was slower than actually creating the stream, the XmlTextReader, and passing
the reader to the XmlDocument.Load();.

I've tried it, and darn if he wasn't right.

System.Xml.XmlDocument DOMObj = new System.Xml.XmlDocument();
System.IO.StreamReader stream = new System.IO.StreamReader
(Server.MapPath("file.xml"));
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(stream);
reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
DOMObj.Load(reader);

was 5% faster than

System.Xml.XmlDocument DOMObj = new System.Xml.XmlDocument();
System.Xml.XmlTextReader reader = new
System.Xml.XmlTextReader(Server.MapPath ("file.xml"));
reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
DOMObj.Load(reader);

which was 20% faster than
System.Xml.XmlDocument DOMObj = new System.Xml.XmlDocument();
DOMObj.PreserveWhitespace = true;
DOMObj.Load(Server.MapPath("file.xml"));

I can't think of any reason why it's so much faster to manually create your
own stream and XmlTextReader rather than letting XmlDocument figure out the
fastest way to process the document.

Any ideas?

Thanks
_mark
 
B

Bruce Barker

doc.load(filename) - creates a validating xml reader, which is why its so
slow

new XmlTextReader(filename) - expects a uri, not a filename. if no protocol
is specified, then its converted to a file based uri (file://filename), then
opened as a uri resource. this transalation requires serveral directory
lookups.

new XmlTextReader(reader) - just read the stream of bytes from the reader.
if you pick one optimized for file opening and reading, its fastest.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top