gpt4 book ai didi

c# - 哪个是性能最好的 : XPathNavigator with XPath vs Linq to Xml with query?

转载 作者:太空狗 更新时间:2023-10-30 00:27:00 24 4
gpt4 key购买 nike

我有一个应用程序,我在其中使用 XPathNavigator 来迭代节点。它工作正常。

但是我想知道如果我使用 LINQ to Xml....

  1. 我会得到什么好处(性能、可维护性)?

  2. 使用 XPath、LINQ to Xml 对性能有何影响?

我使用的是 C#.net、VS 2010,我的 .xml 是中等大小。

最佳答案

只是补充一下这里已经说明的内容,整体性能似乎取决于您实际对相关文档执行的操作。这是我根据比较 XElement 和 XPathNavigator 之间的解析性能的简单实验运行得出的结论。

如果是选择节点,遍历这些节点,读取一些属性值:

  • XElement.Element 比 XElement.CreateNavigator.Select 快近似系数 1.5。
  • XElement.CreateNavigator.Select 更快比 XPathNavigator.Select 大约高出 0.5。
  • XPathNavigator.Select 比 XElement.XPathSelectElement 快大约为 0.5。

另一方面,如果您还读取每个节点的子节点的值,它会变得有点有趣:

  • XElement.Element 比 XElement.XPathSelectElements 快大约 0.5 倍。
  • XElement.XPathSelectElement 比 XPathNavigator.Select 快大约 3 倍。
  • XPathNavigator.Select 比 XElement.CreateNavigator.Select 快大约 0.5 倍。

这些结论基于以下代码:

 [Test]
public void CompareXPathNavigatorToXPathSelectElement()
{
var max = 100000;

Stopwatch watch = new Stopwatch();
watch.Start();

bool parseChildNodeValues = true;

ParseUsingXPathNavigatorSelect(max, watch, parseChildNodeValues);
ParseUsingXElementElements(watch, max, parseChildNodeValues);
ParseUsingXElementXPathSelect(watch, max, parseChildNodeValues);
ParseUsingXPathNavigatorFromXElement(watch, max, parseChildNodeValues);
}

private static void ParseUsingXPathNavigatorSelect(int max, Stopwatch watch, bool parseChildNodeValues)
{
var document = new XPathDocument(@"data\books.xml");
var navigator = document.CreateNavigator();

for (var i = 0; i < max; i++)
{
var books = navigator.Select("/catalog/book");
while (books.MoveNext())
{
var location = books.Current;
var book = new Book();
book.Id = location.GetAttribute("id", "");

if (!parseChildNodeValues) continue;

book.Title = location.SelectSingleNode("title").Value;
book.Genre = location.SelectSingleNode("genre").Value;
book.Price = location.SelectSingleNode("price").Value;
book.PublishDate = location.SelectSingleNode("publish_date").Value;
book.Author = location.SelectSingleNode("author").Value;
}
}

watch.Stop();
Console.WriteLine("Time using XPathNavigator.Select = " + watch.ElapsedMilliseconds);
}

private static void ParseUsingXElementElements(Stopwatch watch, int max, bool parseChildNodeValues)
{
watch.Restart();
var element = XElement.Load(@"data\books.xml");

for (var i = 0; i < max; i++)
{
var books = element.Elements("book");
foreach (var xElement in books)
{
var book = new Book();
book.Id = xElement.Attribute("id").Value;

if (!parseChildNodeValues) continue;

book.Title = xElement.Element("title").Value;
book.Genre = xElement.Element("genre").Value;
book.Price = xElement.Element("price").Value;
book.PublishDate = xElement.Element("publish_date").Value;
book.Author = xElement.Element("author").Value;
}
}

watch.Stop();
Console.WriteLine("Time using XElement.Elements = " + watch.ElapsedMilliseconds);
}

private static void ParseUsingXElementXPathSelect(Stopwatch watch, int max, bool parseChildNodeValues)
{
XElement element;
watch.Restart();
element = XElement.Load(@"data\books.xml");

for (var i = 0; i < max; i++)
{
var books = element.XPathSelectElements("book");
foreach (var xElement in books)
{
var book = new Book();
book.Id = xElement.Attribute("id").Value;

if (!parseChildNodeValues) continue;

book.Title = xElement.Element("title").Value;
book.Genre = xElement.Element("genre").Value;
book.Price = xElement.Element("price").Value;
book.PublishDate = xElement.Element("publish_date").Value;
book.Author = xElement.Element("author").Value;
}
}

watch.Stop();
Console.WriteLine("Time using XElement.XpathSelectElement = " + watch.ElapsedMilliseconds);
}

private static void ParseUsingXPathNavigatorFromXElement(Stopwatch watch, int max, bool parseChildNodeValues)
{
XElement element;
watch.Restart();
element = XElement.Load(@"data\books.xml");

for (var i = 0; i < max; i++)
{
// now we can use an XPath expression
var books = element.CreateNavigator().Select("book");

while (books.MoveNext())
{
var location = books.Current;
var book = new Book();
book.Id = location.GetAttribute("id", "");

if (!parseChildNodeValues) continue;

book.Title = location.SelectSingleNode("title").Value;
book.Genre = location.SelectSingleNode("genre").Value;
book.Price = location.SelectSingleNode("price").Value;
book.PublishDate = location.SelectSingleNode("publish_date").Value;
book.Author = location.SelectSingleNode("author").Value;
}
}

watch.Stop();
Console.WriteLine("Time using XElement.Createnavigator.Select = " + watch.ElapsedMilliseconds);
}

使用从 here 下载的 books.xml

总的来说,如果您的文档有点扁平,看起来 XElement 解析 API(不包括 XPath 扩展)可为您提供最佳性能,同时也更易于使用。如果你有很深的嵌套结构,你必须做类似的事情

XElement.Element("book").Element("author").Element("firstname").SomethingElse()

那么 XElement.XPathSelectElement 可能会在性能和代码可维护性之间提供最佳折衷。

关于c# - 哪个是性能最好的 : XPathNavigator with XPath vs Linq to Xml with query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8530416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com