C# - 对于Foreach - 巨大的性能差异 [英] C# - For vs Foreach - Huge performance difference

查看:140
本文介绍了C# - 对于Foreach - 巨大的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个算法进行了一些优化,在给定的数组中,我发现了一个大于X的最小数字,但是我偶然发现了一个奇怪的差别。在下面的代码中,ForeachUpper在625ms结束,ForUpper结束,我相信,几个小时(疯狂地慢)。为什么要这么做?

  class Teste 
{
public double Valor {get;组; }

public Teste(double d)
{
Valor = d;
}

public override string ToString()
{
returnTeste:+ Valor;
}
}

private static IEnumerable< Teste> GetTeste(double total)
{
for(int i = 1; i <= total; i ++)
{
yield return new Teste(i);


static void Main(string [] args)
{
int total = 1000 * 1000 * 30;
double test = total / 2 + .7;

var ieTeste = GetTeste(total).ToList();


Console.WriteLine(------------);

ForeachUpper(ieTeste.Select(d => d.Valor),test);
Console.WriteLine(------------);
ForUpper(ieTeste.Select(d => d.Valor),test);
Console.Read();


private static void ForUpper(IEnumerable< double> bigList,double find)
{
var start1 = DateTime.Now;

double uppper = 0;
for(int i = 0; i< bigList.Count(); i ++)
{
var toMatch = bigList.ElementAt(i);
if(toMatch> = find)
{
uppper = toMatch;
break;
}
}

var end1 =(DateTime.Now - start1).TotalMilliseconds;

Console.WriteLine(end1 +=+ uppper);


private static void ForeachUpper(IEnumerable< double> bigList,double find)
{
var start1 = DateTime.Now;

double upper = 0;
foreach(var toMatch in bigList)
{
if(toMatch> = find)
{
upper = toMatch;
break;
}
}

var end1 =(DateTime.Now - start1).TotalMilliseconds;

Console.WriteLine(end1 +=+ upper);





谢谢

code> IEnumerable< T> 不可索引。
$ b $ < $ c> Count()和 ElementAt()您在的每次迭代中调用的扩展方法 loop是O(n);他们需要遍历集合来查找count或第n个元素。



道德:了解您的集合类型。 b $ b

i was making some optimizations to an algorithm that finds the smallest number that is bigger than X, in a given array, but then a i stumbled on a strange difference. On the code bellow, the "ForeachUpper" ends in 625ms, and the "ForUpper" ends in, i believe, a few hours (insanely slower). Why so?

  class Teste
{
    public double Valor { get; set; }

    public Teste(double d)
    {
        Valor = d;
    }

    public override string ToString()
    {
        return "Teste: " + Valor;
    }
}

  private static IEnumerable<Teste> GetTeste(double total)
    {
        for (int i = 1; i <= total; i++)
        {
            yield return new Teste(i);
        }
    }
    static void Main(string[] args)
    {
        int total = 1000 * 1000*30 ;
        double test = total/2+.7;

        var ieTeste = GetTeste(total).ToList();


        Console.WriteLine("------------");

        ForeachUpper(ieTeste.Select(d=>d.Valor), test);
        Console.WriteLine("------------");
        ForUpper(ieTeste.Select(d => d.Valor), test);
        Console.Read();
    }

    private static void ForUpper(IEnumerable<double> bigList, double find)
    {
        var start1 = DateTime.Now;

        double uppper = 0;
        for (int i = 0; i < bigList.Count(); i++)
        {
            var toMatch = bigList.ElementAt(i);
            if (toMatch >= find)
            {
                uppper = toMatch;
                break;
            }
        }

        var end1 = (DateTime.Now - start1).TotalMilliseconds;

        Console.WriteLine(end1 + " = " + uppper);
    }

    private static void ForeachUpper(IEnumerable<double> bigList, double find)
    {
        var start1 = DateTime.Now;

        double upper = 0;
        foreach (var toMatch in bigList)
        {
            if (toMatch >= find)
            {
                upper = toMatch;
                break;
            }
        }

        var end1 = (DateTime.Now - start1).TotalMilliseconds;

        Console.WriteLine(end1 + " = " + upper);
    }

Thanks

解决方案

IEnumerable<T> is not indexable.

The Count() and ElementAt() extension methods that you call in every iteration of your for loop are O(n); they need to loop through the collection to find the count or the nth element.

Moral: Know thy collection types.

这篇关于C# - 对于Foreach - 巨大的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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