"嵌套的foreach" VS"波长/ LINQ查询"性能(LINQ到对象) [英] "Nested foreach" vs "lambda/linq query" performance(LINQ-to-Objects)

查看:139
本文介绍了"嵌套的foreach" VS"波长/ LINQ查询"性能(LINQ到对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在性能来看应该怎么使用嵌套的foreach的或拉姆达/ LINQ查询?

In performance point of view what should you use "Nested foreach's" or "lambda/linq queries"?

推荐答案

写清楚的code可以,然后基准和配置文件来发现任何性能问题。如果你的的有性能问题,你可以用不同的code试验,以找出是否它的速度更快与否(测量所有的时间作为真实的数据可能),然后作出判断呼叫作为在性能的提高是否值得打的可读性。

Write the clearest code you can, and then benchmark and profile to discover any performance problems. If you do have performance problems, you can experiment with different code to work out whether it's faster or not (measuring all the time with as realistic data as possible) and then make a judgement call as to whether the improvement in performance is worth the readability hit.

直接的foreach 方法的将会的在许多情况下比LINQ速度更快。例如,考虑:

A direct foreach approach will be faster than LINQ in many cases. For example, consider:

var query = from element in list
            where element.X > 2
            where element.Y < 2
            select element.X + element.Y;

foreach (var value in query)
{
    Console.WriteLine(value);
}

现在有两个,其中子句和选择条款,所以每一个最终的项目必须经过三个迭代器。 (显然这两个where子句可以在这种情况下合并,但我在做一般点。)

Now there are two where clauses and a select clause, so every eventual item has to pass through three iterators. (Obviously the two where clauses could be combined in this case, but I'm making a general point.)

现在与直接code比较吧:

Now compare it with the direct code:

foreach (var element in list)
{
    if (element.X > 2 && element.Y < 2)
    {
        Console.WriteLine(element.X + element.Y);
    }
}

这将运行得更快,因为它有较少的箍通过运行。机会是,控​​制台输出会矮虽然迭代器的成本,我当然会preFER LINQ查询。

That will run faster, because it has fewer hoops to run through. Chances are that the console output will dwarf the iterator cost though, and I'd certainly prefer the LINQ query.

编辑:为了回答关于嵌套的foreach循环...与的SelectMany psented通常那些被重新$ P $或第二子句:

To answer about "nested foreach" loops... typically those are represented with SelectMany or a second from clause:

var query = from item in firstSequence
            from nestedItem in item.NestedItems
            select item.BaseCount + nestedItem.NestedCount;

在这里,我们只是由于嵌套的的foreach 循环增加一个额外的迭代,因为我们就已经在使用的第一个序列中的每个项目一个额外的迭代器。还是有一些开销,包括在委托的,而不是内联做投影的开销(这是我之前没有提到),但它仍然不会是在嵌套的foreach性能非常不同的。

Here we're only adding a single extra iterator, because we'd already be using an extra iterator per item in the first sequence due to the nested foreach loop. There's still a bit of overhead, including the overhead of doing the projection in a delegate instead of "inline" (something I didn't mention before) but it still won't be very different to the nested-foreach performance.

这是不是说你不能拍自己的脚与LINQ,当然。你可以写非常多产低效的查询,如果你不先搞你的大脑 - 但是这远非唯一的LINQ ...

This is not to say you can't shoot yourself in the foot with LINQ, of course. You can write stupendously inefficient queries if you don't engage your brain first - but that's far from unique to LINQ...

这篇关于&QUOT;嵌套的foreach&QUOT; VS&QUOT;波长/ LINQ查询&QUOT;性能(LINQ到对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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