Linq性能解析XML文件 [英] Linq performance parsing XML file

查看:84
本文介绍了Linq性能解析XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析一个XML文件(1〜10 MB);我正在为此目的使用XDocument.

I need to parse an XML file (1~10 MB); I am using XDocument for the purpose.

目前,我正在使用Linq查询XML文档,但是我需要提高应用程序的性能,因此我想将Linq查询替换为旧样式循环,但我没有任何帮助.

At the moment I am using Linq to query the XML document, but I need to improve the performance of my application and I thought to replace the Linq query with old style loops, but I did not have any boost.

以下是一段查询次数最多的代码:

Following is a piece of code with the most called query:

Stopwatch stopwatch = new Stopwatch();
XDocument xdoc = XDocument.Load("filename.xml");
string def = "Version";
XElement xelm;

stopwatch.Start();

for (int i = 0; i < 1000; i++)
    xelm = xdoc.Descendants("def").Where(d => d.Attribute("name").Value == def).Single();

stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);


stopwatch.Restart();

for (int i = 0; i < 1000; i++)
{
    foreach (var elm in xdoc.Descendants("def"))
    {
        if (elm.Attribute("name").Value == def)
        {
            xelm = elm;
            break;
        }
    }
}

stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);

两个版本的耗用时间几乎相同,对我来说,这个结果非常奇怪,因为我认为LinqWhere方法在调用时必须创建一个新列表.

The elapsed time is pretty much the same for both versions, and for me this result is quite strange since I thought that the Where method of Linq has to create a new list when invoked.

为什么两个版本具有相同的性能?有没有办法改善原始代码?

Why both versions have the same performace? Is there a way to improve the original code?

推荐答案

LINQ使用延迟执行(延迟),这意味着它仅在一次集合中进行迭代,并且仅在需要时进行迭代.

LINQ uses deferred (lazy) execution, which means that it only iterates through collections once and only when it needs to.

当您必须处理大型数据集合时,尤其是在包含一系列链接的查询或操作的程序中,延迟执行可以大大提高性能.最好的情况是,延迟执行只能在源集合中进行一次迭代.

Deferred execution can greatly improve performance when you have to manipulate large data collections, especially in programs that contain a series of chained queries or manipulations. In the best case, deferred execution enables only a single iteration through the source collection.

在这种情况下,Where子句不会生成任何新列表.实际上,LINQ语句的编译版本的功能几乎与foreach语句相同.

In this case, there is no new list being generated from your Where clause. In fact, the compiled version of your LINQ statement will function in almost the same way as your foreach statement.

我唯一想提高性能的想法是遵循Robert McKee的回答,该回答说您应该使用.First()而不是.Single()来不必遍历整个列表.除此之外,我不确定您可以还能做什么,除了使用不同的库或不同的语言.

My only idea about improving performance is to follow Robert McKee's answer, which says that you should use .First() instead of .Single() to not have to iterate through your entire list. Beyond that, I'm not sure what else you can do, except use a different library or a different language.

这篇关于Linq性能解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆