是IEnumerable.Any比与休息循环更快? [英] Is IEnumerable.Any faster than a for loop with a break?

查看:307
本文介绍了是IEnumerable.Any比与休息循环更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们经历了我们的代码中的一些缓慢打开窗体,这是可能是由于用断裂循环这是花费很长的时间来执行。我这个切换到 IEnumerable.Any(),看到的形式非常迅速打开。现在我想弄清楚,如果做单是这种变化提高性能或如果它是更有效地访问 ProductIDs 属性。如果此实现更快,如果是的话,为什么



原实:

 公共BOOL containsProduct(INT的productID){
布尔containsProduct = FALSE;
的for(int i = 0; I< this.ProductIDs.Length;我++){
如果(==的productID this.ProductIDs [I]){
containsProduct = TRUE;
中断;
}
}
返回containsProduct;
}



新的实现:

 公共BOOL ContainsProduct(INT的productID){
返回this.ProductIDs.Any(T => ==的productID T);
}


解决方案

首先实施的稍快(枚举比稍慢为循环)。第二个是的很多更具可读性






更新



俄德的答案是正确的可能和它察觉做得很好。第一个是慢在这里,因为它涉及到数据库的往返。否则,稍快如我所说






更新2 - 证明



下面是显示一个简单的代码,为什么第一个是速度快:

 公共静态无效的主要( )
{

INT [] =值Enumerable.Range(0,1000000).ToArray();

INT哑= 0;
秒表=新的秒表();

stopwatch.Start();
的for(int i = 0; I< values.Length;我++)
{
假人* = I;
}
stopwatch.Stop();
Console.WriteLine(循环了{0},stopwatch.ElapsedTicks);

假人= 0;
stopwatch.Reset();
stopwatch.Start();
的foreach(中值VAR值)
{
虚* =价值;
}
stopwatch.Stop();
Console.WriteLine(迭代了{0},stopwatch.ElapsedTicks);

Console.Read();
}

下面是输出:




循环了12198



迭代了20922




所以循环两次是快迭代/枚举。


We experienced some slowness in our code opening a form and it was possibly due to a for loop with a break that was taking a long time to execute. I switched this to an IEnumerable.Any() and saw the form open very quickly. I am now trying to figure out if making this change alone increased performance or if it was accessing the ProductIDs property more efficiently. Should this implementation be faster, and if so, why?

Original Implementation:

public bool ContainsProduct(int productID) {
    bool containsProduct = false;
    for (int i = 0; i < this.ProductIDs.Length; i++) {
        if (productID == this.ProductIDs[i]) {
            containsProduct = true;
            break;
        }
    }
    return containsProduct;
}

New Implementation:

public bool ContainsProduct(int productID) {
    return this.ProductIDs.Any(t => productID == t);
}

解决方案

First implementation is slightly faster (enumeration is slightly slower than for loop). Second one is a lot more readable.


UPDATE

Oded's answer is possibly correct and well done for spotting it. The first one is slower here since it involves database roundtrip. Otherwise, it is slightly faster as I said.


UPDATE 2 - Proof

Here is a simple code showing why first one is faster:

    public static void Main()
    {

        int[] values = Enumerable.Range(0, 1000000).ToArray();

        int dummy = 0;
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        for (int i = 0; i < values.Length; i++)
        {
            dummy *= i;
        }
        stopwatch.Stop();
        Console.WriteLine("Loop took {0}", stopwatch.ElapsedTicks);

        dummy = 0;
        stopwatch.Reset();
        stopwatch.Start();
        foreach (var value in values)
        {
            dummy *= value;         
        }
        stopwatch.Stop();
        Console.WriteLine("Iteration took {0}", stopwatch.ElapsedTicks);

        Console.Read();
    }

Here is output:

Loop took 12198

Iteration took 20922

So loop is twice is fast as iteration/enumeration.

这篇关于是IEnumerable.Any比与休息循环更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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