473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XmlDocument vs XPathDocument performance

Hi...

I've been trying to improve the performance of our xml handling, so I've
been doing some timing tests lately and ran across something I thought odd -
XPathDocument seems slightly slower than XmlDocument. I've run the tests
several times and have tried to eliminate extraneous variables like disk
access but every time, XPathDocument comes up slower by a few percent.

I'd expected it to be faster since it's a read-only representation. Am I
doing something wrong?

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
StringReader sr = new StringReader(xmldoc);
doc = new XmlDocument();
doc.Load(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
StringReader sr = new StringReader(xmldoc);
pDoc = new XPathDocument(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

In other tests, XPathDocument seemed to improve
XslCompiledTransform.Transform performance very slightly, but not enough to
justify the switch given that the loading seemed more expensive...

Thanks
Mark

Jun 27 '08 #1
5 6242
Hi Mark,

As for the XPathDocument, it has different implementation internally. You
can use reflector to check its constructor. It use an Additional
XPathDocumentBuilder and the loading process is also a bit more complex
than the XmlDocument. I think this is probably due to the specific
implementation required for XPathNaviagtor generation.

For your scenario, if you haven't particular XPath based navigation
requirement, a normal XmlDocument is enough for standard DOM based
read/edit.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.--------------------
>From: =?Utf-8?B?TWFyaw==?= <mm******@nospam.nospam>
Subject: XmlDocument vs XPathDocument performance
Date: Thu, 5 Jun 2008 08:56:03 -0700
>
Hi...

I've been trying to improve the performance of our xml handling, so I've
been doing some timing tests lately and ran across something I thought odd
-
>XPathDocument seems slightly slower than XmlDocument. I've run the tests
several times and have tried to eliminate extraneous variables like disk
access but every time, XPathDocument comes up slower by a few percent.

I'd expected it to be faster since it's a read-only representation. Am I
doing something wrong?

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
StringReader sr = new StringReader(xmldoc);
doc = new XmlDocument();
doc.Load(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
StringReader sr = new StringReader(xmldoc);
pDoc = new XPathDocument(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

In other tests, XPathDocument seemed to improve
XslCompiledTransform.Transform performance very slightly, but not enough
to
>justify the switch given that the loading seemed more expensive...

Thanks
Mark

Jun 27 '08 #2
Hi Steven...

There are a number of cases, such as applying xsl stylesheets where we don't
require the source to be read/write. I'd assumed that since XPathDocument
wouldn't have to support the write side of things that it might be faster for
read-only type applications. I was surprised to find that if anything it
seemed slightly slower, and that was odd to me.

I can understand given the widespread use that a *lot* of effort went into
optimizing XmlDocument, but I was looking to shave a few cycles going
read-only where I could. I was just surprised that it went the other way.

Thanks
Mark
"Steven Cheng [MSFT]" wrote:
Hi Mark,

As for the XPathDocument, it has different implementation internally. You
can use reflector to check its constructor. It use an Additional
XPathDocumentBuilder and the loading process is also a bit more complex
than the XmlDocument. I think this is probably due to the specific
implementation required for XPathNaviagtor generation.

For your scenario, if you haven't particular XPath based navigation
requirement, a normal XmlDocument is enough for standard DOM based
read/edit.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.--------------------
From: =?Utf-8?B?TWFyaw==?= <mm******@nospam.nospam>
Subject: XmlDocument vs XPathDocument performance
Date: Thu, 5 Jun 2008 08:56:03 -0700

Hi...

I've been trying to improve the performance of our xml handling, so I've
been doing some timing tests lately and ran across something I thought odd
-
XPathDocument seems slightly slower than XmlDocument. I've run the tests
several times and have tried to eliminate extraneous variables like disk
access but every time, XPathDocument comes up slower by a few percent.

I'd expected it to be faster since it's a read-only representation. Am I
doing something wrong?

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
StringReader sr = new StringReader(xmldoc);
doc = new XmlDocument();
doc.Load(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
StringReader sr = new StringReader(xmldoc);
pDoc = new XPathDocument(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

In other tests, XPathDocument seemed to improve
XslCompiledTransform.Transform performance very slightly, but not enough
to
justify the switch given that the loading seemed more expensive...

Thanks
Mark

Jun 27 '08 #3

"Mark" <mm******@nospam.nospamwrote in message
news:DB**********************************@microsof t.com...
Hi Steven...

There are a number of cases, such as applying xsl stylesheets where we
don't
require the source to be read/write. I'd assumed that since XPathDocument
wouldn't have to support the write side of things that it might be faster
for
read-only type applications. I was surprised to find that if anything it
seemed slightly slower, and that was odd to me.

I can understand given the widespread use that a *lot* of effort went into
optimizing XmlDocument, but I was looking to shave a few cycles going
read-only where I could. I was just surprised that it went the other way.
I'm not sure why there not being code present to perform write operations
would have any impact on read performance.

Have you tried examining the memory requirements between the two of them?

I haven't done that myself but I suspect XPathDocument's true strength lies
in its application in a multi-threaded environment such as ASP.NET where
many such documents are being loaded and read. This where memory usage is
likely to have a greater impact on performance than sheer CPU grunt.

--
Anthony Jones - MVP ASP/ASP.NET
Jun 27 '08 #4

"Anthony Jones" wrote:
I'm not sure why there not being code present to perform write operations
would have any impact on read performance.

Have you tried examining the memory requirements between the two of them?

I haven't done that myself but I suspect XPathDocument's true strength lies
in its application in a multi-threaded environment such as ASP.NET where
many such documents are being loaded and read. This where memory usage is
likely to have a greater impact on performance than sheer CPU grunt.
I haven't profiled the memory requirements either, but the theory (perhaps
half-baked) was that a read-only implementation would be able to jettison a
lot of code paths and have fewer checks in the existing methods that go to
support write operations.

I know it's not quite apples to apples, but I thought XPathDocument might be
half way to a sax implementation in terms of being lighter weight and faster.

My first tests included doing a transform on the same ~3k xml document.
Then I separated the times for loading and transforming. XPathDocument was
very slightly faster on transforms but not enough to balance the extra
slowness of getting a loaded instantiation of it.

As I said, that came as a bit of a surprise to me.

Mark
Jun 27 '08 #5
Hi Mark,

Yes, theoretically XPathDocument only perform read access, however, I think
the Xpath specific implementation here add the most overhead. So far for
the xml API in .net framework, the xml reader/writer is the most light
weight and effecient one(a customized SAX model).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.--------------------
>Subject: Re: XmlDocument vs XPathDocument performance
Date: Sat, 7 Jun 2008 10:31:00 -0700
>

"Anthony Jones" wrote:
>I'm not sure why there not being code present to perform write operations
would have any impact on read performance.

Have you tried examining the memory requirements between the two of them?

I haven't done that myself but I suspect XPathDocument's true strength
lies
>in its application in a multi-threaded environment such as ASP.NET where
many such documents are being loaded and read. This where memory usage
is
>likely to have a greater impact on performance than sheer CPU grunt.

I haven't profiled the memory requirements either, but the theory (perhaps
half-baked) was that a read-only implementation would be able to jettison
a
>lot of code paths and have fewer checks in the existing methods that go to
support write operations.

I know it's not quite apples to apples, but I thought XPathDocument might
be
>half way to a sax implementation in terms of being lighter weight and
faster.
>
My first tests included doing a transform on the same ~3k xml document.
Then I separated the times for loading and transforming. XPathDocument
was
>very slightly faster on transforms but not enough to balance the extra
slowness of getting a loaded instantiation of it.

As I said, that came as a bit of a surprise to me.

Mark
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ian Lawton | last post by:
Hi, in a C# app, I have an XPathDocument that looks similar to this: <row uniqueid="4234" /> <row uniqueid="4365" /> <row uniqueid="3124" /> <row uniqueid="9879" /> <row uniqueid="1332" />...
2
by: Jim Lewis | last post by:
After everything I have read and some of my own testing I am convinced that XPathDocument are more efficient if you are only using XML for read only and not modifying the XML. However, I have been...
2
by: Ed S | last post by:
Hi All, I'd like to convert from an XmlDocument to an XPathDocument (without saving the XmlDocument down to a file - this is a web app). I can't see any way to do this. Any help is...
2
by: Graham Pengelly | last post by:
Hi I am trying to transform on System.Xml.XmlDocument into another using XslTransform without writing the object out to a file. I am guessing it should work something like this... public...
3
by: Fahad Ashfaque | last post by:
Hi, I found the XSL Transformation through .NET too complex. The most simplest overload of System.Xml.Xsl.XslTransform is Transform(inputfile, outputfile), but this saves the result into file. I...
5
by: Fahad Ashfaque | last post by:
Hi, I've an object of XmlDocument, I need to pass it to some function which takes XpathDocument object, How could I convert? There is not Conversion Operator overload like that. Your reply...
1
by: Greg Allen | last post by:
Is there a way to get line number information from an XmlDocument object? As I work with the various elements in the XmlDocument, I want to be able to refer to the line number. I found an...
5
by: Ben R. | last post by:
Hi, Could someone explain the functional difference between these classes? From what I understand, xpathdocument is faster in some scenarios, but I'm not sure why. Further, why is it that the...
1
by: John A Grandy | last post by:
I've got an app that has hundreds of medium-sized (100s of elements) XML files on disk (not in db). Right now these are loaded via XMLDocument.Load and searched with XPATH. The performance has...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.