Count number of element in List>

前端 未结 4 1303
野性不改
野性不改 2021-02-12 07:53

I have a List>. How can I count all the elements in this as if it was a single List in the fastest way?

So far I h

4条回答
  •  后悔当初
    2021-02-12 08:32

    I'd like to get the chance to answer this question just to highlight when we should use linq and when a classic for. Unfortunately today people doesn’t care a lot about performance as we got use to work on very powerful computer. Anyway just try the code below and you will discover that Linq is more then 100 times slower than the classic for version. You should use Linq only when the expression you need to write is really complex and you want make it more readable. I didn't spend time to the solution shoed below as I'd like to focus on the performance

    public static void Main(string [] arg)
    {
        //create the list
        List> listOfList = new List>()
                                          {
                                              new List()
                                                  {
                                                      "1.1","2.2"
                                                  }
                                          ,
                                           new List()
                                                  {
                                                      "2.1","2.2","2.3"
                                                  }
                                          };
        //stopwatch using Linq
        Stopwatch stopwatch=new Stopwatch();
        stopwatch.Start();
    
        int totalUsingLinq = listOfList.Sum(x => x.Count);
    
        stopwatch.Stop();
        Console.WriteLine("Using Linq:{0}",stopwatch.Elapsed); //00005713
    
        int totalUsingFor = 0;
        //stopwatch using classic for 
        stopwatch.Reset();
        stopwatch.Start();
        totalUsingFor = 0;
        for(int i=0;i

    distinct version using for (just for example). In this case I have create a very "bottleneck" function that does the distinct and it is still faster.

     public class Program
        {
          public static void Main(string[] arg)
            {
                //create the list
                List> listOfList = new List>()
                                          {
                                              new List()
                                                  {
                                                      "1.1","2.2","1.1","1.1","2.2","1.1","1.1","2.2","1.1","1.1"
                                                  }
                                          ,
                                           new List()
                                                  {
                                                      "2.1","2.2","2.3","2.3","1.1","2.2","1.1","1.1","2.2","1.1","1.1","2.2","1.1","1.1","2.2","1.1","1.1","2.2","1.1"
                                                  }
                                          };
                //stopwatch using Linq
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
    
                int totalUsingLinq = listOfList.Sum(l => l.Distinct().Count());
    
    
                stopwatch.Stop();
                Console.WriteLine("Using Linq:{0}", stopwatch.Elapsed); //000012150    
                int totalUsingFor = 0;
                //stopwatch using classic for 
                stopwatch.Reset();
                stopwatch.Start();
                totalUsingFor = 0;
                for (int i = 0; i < listOfList.Count; i++)
                {
                    var mainItem = listOfList[i];
                    if (mainItem != null)
                    {
                        for(int y=0;y list)
            {
                var item = list[index];
                for(int i=index+1;i

提交回复
热议问题