Is the LINQ Count() method any faster or slower than List<>.Count or Array.Length?
Is the LINQ Count() method any faster or slower than List<>.Count or Array.Length ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In general slower. LINQ’s Count in general is an
O(N)operation whileList.CountandArray.Lengthare both guaranteed to beO(1).However it some cases LINQ will special case the
IEnumerable<T>parameter by casting to certain interface types such asIList<T>orICollection<T>. It will then use that Count method to do an actualCount()operation. So it will go back down toO(1). But you still pay the minor overhead of the cast and interface call.