Are LINQ .Last, .Skip, etc. methods optimized for arrays or List<T>?
I'm wondering if LINQ methods like .Last
and .Skip
are optimized for arrays, List<T>
and such. E.g. for an array I could do _array[_array.Length - 1]
to get the last element.开发者_如何学Python Does _array.Last()
actually enumerate through all elements and then return the last or is there actually some optimization built in?
Might have to forgo fluency for performance if not.
Last()
is optimized when there isn't a predicate... it could be optimized even if there is a predicate (by working back from the end), but it isn't.
I don't think Skip
is optimized - although again, it could be.
Basically most of LINQ to Objects is optimized where it can be (for ICollection<T>
, ICollection
, and IList<T>
) but there's still room for more to come.
精彩评论