Is the Linq Count() faster or slower than List.Count or Array.Length?

前端 未结 7 517
你的背包
你的背包 2020-11-29 04:59

Is the LINQ Count() method any faster or slower than List<>.Count or Array.Length?

相关标签:
7条回答
  • 2020-11-29 05:22

    I believe that if you call Linq.Count() on either an ICollection or IList (like an ArrayList or List) then it will just return the Count property's value. So the performance will be about the same on plain collections.

    0 讨论(0)
  • 2020-11-29 05:23

    Some additional info - LINQ Count - the difference between using it and not can be huge - and this doesn't have to be over 'large' collections either. I have a collection formed from linq to objects with about 6500 items (big.. but not huge by any means) . Count() in my case takes several seconds. Converting to a list (or array, whatver) the count is then virtually immediate. Having this count in an inner loop means the impact could be huge. Count enumerates through everything. An array and a list are both 'self aware' of their lengths and do not need to enumerate them. Any debug statements (log4net for ex) that reference this count() will also then slow everything down considerably more. Do yourself a favor and if you need to reference this often save the count size and only call it once on a LINQ collection unless you convert it to a list and then can reference away without a performance hit.

    Here is a quick test of what I was talking about above. Note every time we call Count() our collection size changes.. hence evaluation happens, which is more than an expected 'count' operation. Just something to be aware of : )

        
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
    
        namespace LinqTest
        {
            class TestClass
            {
                public TestClass()
                {
                    CreateDate = DateTime.Now;
                }
                public DateTime CreateDate;
            }
    
            class Program
            {
    
                static void Main(string[] args)
                {
                    //Populate the test class
                    List list = new List(1000);
                    for (int i=0; i<1000; i++)
                    {
                        System.Threading.Thread.Sleep(20);
                        list.Add(new TestClass());
                        if(i%100==0)
                        { 
                            Console.WriteLine(i.ToString() +  " items added");
                        }
                    }
    
                    //now query for items 
                    var newList = list.Where(o=> o.CreateDate.AddSeconds(5)> DateTime.Now);
                    while (newList.Count() > 0)
                    {
                        //Note - are actual count keeps decreasing.. showing our 'execute' is running every time we call count.
                        Console.WriteLine(newList.Count());
                        System.Threading.Thread.Sleep(500);
                    }
                }
            }
        }
    
    
    0 讨论(0)
  • 2020-11-29 05:33

    The Enumerable.Count() method checks for ICollection<T>, using .Count - so in the case of arrays and lists, it is not much more inefficient (just an extra level of indirection).

    0 讨论(0)
  • 2020-11-29 05:35

    List.Count or Array.Length is indeed faster than Linq Count(). Because Linq Count() will iterate through the whole list of items to count. List.Count or Array.Length use their property.

    0 讨论(0)
  • 2020-11-29 05:40

    In general Slower. LINQ's Count in general is an O(N) operation while List.Count and Array.Length are both guaranteed to be O(1).

    However it some cases LINQ will special case the IEnumerable<T> parameter by casting to certain interface types such as IList<T> or ICollection<T>. It will then use that Count method to do an actual Count() operation. So it will go back down to O(1). But you still pay the minor overhead of the cast and interface call.

    0 讨论(0)
  • 2020-11-29 05:45

    I would say it depends on the List. If it is an IQueryable that is a table in a db somewhere then Count() will be much faster because it doesn't have to load all of the objects. But if the list is in-memory i would guess that the Count property would be faster if not about the same.

    0 讨论(0)
提交回复
热议问题