Linq Lambda vs Query Syntax Performance

后端 未结 2 2047
悲&欢浪女
悲&欢浪女 2021-01-08 00:38

i saw today a linq query syntax in my project which was counting from List items on specifc condition this way:

int temp =  (from A in pTasks 
          


        
2条回答
  •  时光取名叫无心
    2021-01-08 01:31

    Like Farhad said, the implementation of Where(x).Count() and Count(x) vary. The first one instantiates an additional iterator, which on my pc costs about 30.000 ticks (regardless of the collection size)

    Also, ToList is not free. It allocates memory. It costs time. On my pc, it roughly doubles execution time. (so linear dependent op the collection size)

    Also, debugging requires spin-up time. So it's difficult to acurately measure performace in one go. I'd recommend a loop like this example. Then, ignore the first set of results.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var pTasks = Task.GetTasks();
                for (int i = 0; i < 5; i++)
                {
    
                    var s1 = Stopwatch.StartNew();
                    var count1 = pTasks.Count(x => x.StatusID == (int) BusinessRule.TaskStatus.Pending);
                    s1.Stop();
                    Console.WriteLine(s1.ElapsedTicks);
    
                    var s2 = Stopwatch.StartNew();
                    var count2 =
                        (
                            from A in pTasks
                            where A.StatusID == (int) BusinessRule.TaskStatus.Pending
                            select A
                            ).ToList().Count();
                    s2.Stop();
                    Console.WriteLine(s2.ElapsedTicks);
    
                    var s3 = Stopwatch.StartNew();
                    var count3 = pTasks.Where(x => x.StatusID == (int) BusinessRule.TaskStatus.Pending).Count();
                    s3.Stop();
                    Console.WriteLine(s3.ElapsedTicks);
    
    
                    var s4 = Stopwatch.StartNew();
                    var count4 =
                        (
                            from A in pTasks
                            where A.StatusID == (int) BusinessRule.TaskStatus.Pending
                            select A
                            ).Count();
                    s4.Stop();
                    Console.WriteLine(s4.ElapsedTicks);
    
                    var s5 = Stopwatch.StartNew();
                    var count5 = pTasks.Count(x => x.StatusID == (int) BusinessRule.TaskStatus.Pending);
                    s5.Stop();
                    Console.WriteLine(s5.ElapsedTicks);
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
    
        public class Task
        {
            public static IEnumerable GetTasks()
            {
                for (int i = 0; i < 10000000; i++)
                {
                    yield return new Task { StatusID = i % 3 };
                }
            }
    
            public int StatusID { get; set; }
        }
    
        public class BusinessRule
        {
            public enum TaskStatus
            {
                Pending,
                Other
            }
        }
    }
    

提交回复
热议问题