。最大()与OrderByDescending()。第一个() [英] .Max() vs OrderByDescending().First()

查看:1105
本文介绍了。最大()与OrderByDescending()。第一个()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这纯粹是我自己的知识,如果我打算写的代码,我只想用。最大()



目前首先想到的。最大()只需要做通过数字单传找到最大,而第二个方式有排序整个事情枚举然后找到的第一个。所以这是 O(N) VS 为O(n LG N)。但后来我想也许它知道它只需要最高的,只是抓住它



问:
是LINQ和/或编译器足够聪明弄清楚,它并不需要梳理整个枚举上下沸腾的代码基本上是一样的。最大()? ?有没有找到一个可以量化的方式

 的IEnumerable< INT>数= Enumerable.Range(1,1000); 

INT最大= numbers.Max();
INT MAX2 = numbers.OrderByDescending(X => X)。首先();


解决方案

如果你正在谈论直LINQ到对象,然后没有,它没有优化的。



据推测另一个LINQ提供程序可以做到,但是这完全取决于执行的细节。



有关枚举的,即反射给了我实现是:



 公共静态IOrderedEnumerable< TSource>排序依据< TSource,TKEY的>(这个IEnumerable的< TSource>源,Func键< TSource,TKEY的>的KeySelectors)
{
返回新OrderedEnumerable< TSource,TKEY的>(源,的KeySelectors,空,假);
}



第一()

 公共静态TSource首先< TSource>(这个IEnumerable的< TSource>源)
{
如果(来源== NULL)
{
掷Error.ArgumentNull(源);
}
&IList的LT; TSource>列表=源作为IList的< TSource取代;
如果(名单!= NULL)
{
如果(list.Count大于0)
{
返回列表[0];
}
} $ B使用$ b,否则
{
(IEnumerator的< TSource>枚举= source.GetEnumerator())
{
如果(枚举.MoveNext())
{
返回enumerator.Current;
}
}
}
掷Error.NoElements();
}


This is purely for my own knowledge, if I were going to write the code I would just use .Max().

At first thought .Max() only has to do a single pass through numbers to find the max, while the second way has to sort the entire thing enumerable then find the first one. So it's O(n) vs O(n lg n). But then I was thinking maybe it knows it only needs the highest and just grabs it.

Question: Is LINQ and/or the compiler smart enough to figure out that it doesn't need to sort the entire enumerable and boils the code down to essentially the same as .Max()? Is there a quantifiable way to find out?

IEnumerable<int> numbers = Enumerable.Range(1, 1000);

int max  = numbers.Max();
int max2 = numbers.OrderByDescending(x => x).First();

解决方案

If you are talking about straight LINQ to Objects, then no, it doesn't optimize for that.

Presumably another LINQ provider could do, but that's up to the particulars of the implementation.

For Enumerable, the implementations that Reflector gives me are:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    return new OrderedEnumerable<TSource, TKey>(source, keySelector, null, false);
}

and for First()

public static TSource First<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    IList<TSource> list = source as IList<TSource>;
    if (list != null)
    {
        if (list.Count > 0)
        {
            return list[0];
        }
    }
    else
    {
        using (IEnumerator<TSource> enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                return enumerator.Current;
            }
        }
    }
    throw Error.NoElements();
}

这篇关于。最大()与OrderByDescending()。第一个()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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