Loser Coder
Loser Coder

Reputation: 2428

Linq performance: Which query is faster

Does anyone know which is more efficient/faster. What would be a good way to test this myself, I don't have large XML documents (<500 KB, not sure if that is large or small) but I have to write these statements over and over again in the code, so wondering which is better/optimal.

XDocument doc = XDocument.Load(file);

doc.Root.Element("childNode").Value;

or

doc.Element("rootNode").Element("childNode").Value ;

Another one:

doc.Root.Elements("childNodes");

vs.

doc.Element("rootNode).Elements("childNodes");

vs.

doc.Element("rootNode").Descendants("childNodes"); 

vs.

doc.Root.Descendants("childNodes") ;

When comparing:

doc.XPathSelectElement("/xpath").Value

is it any faster than the DOM method i.e

XMLDocument dom = new XMLDocument();
dom.LoadXml(input);
dom.SelectSingleNode("/xpath").Value 

Upvotes: 2

Views: 297

Answers (1)

DaveShaw
DaveShaw

Reputation: 52798

You can profile this yourself using the Stopwatch class, or if it's really important, look into tools such as Ants Profiler which will give you some proper metrics.

Upvotes: 2

Related Questions