IEnumerable<T>.Last()是否针对List<T>进行了优化?

6

我有一个包含N个项目的List<T>,名为L

L.Last(),即IEnumerable<T>扩展方法,是否会在线性时间内运行遍历所有N个项目?

还是它内部优化为具有L[L.Count - 1]的常量时间性能?


2
是的。Last函数是Loop的糖果包装器。里面没有魔法。 - Imad Alazani
1个回答

11

你是正确的,如果你看一下实现Last的代码(从Reflector):

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

它实际上是通过返回 list[count - 1]; 来针对 List<T> 进行优化。


即使在这段代码中没有进行优化,如果JIT编译器有足够的信息,它仍然可以潜在地优化该调用。 - ta.speot.is
对我来说,有趣的是它没有针对ICollection<T>.Count进行优化(考虑到IList<T> : ICollection<T>)。这让人感到困惑。 - Brad Christie
2
@BradChristie ICollection<T> 没有索引器 - ta.speot.is
代码中正是我所期望看到的。感谢您澄清了这一点。 - Timothy Shields
1
@PKKG Reflector 是一款付费应用程序,而ILSpydotPeek则是两个免费的替代品。 - Scott Chamberlain
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接