Wednesday, March 13, 2013

LINQ is for querying rather than updating..

LINQ is for querying rather than updating. However, that can be used to build a new list:

Stopwatch s = new Stopwatch();
            s.Start();

            cndSegmentList.FindAll(a => a is PublicRecord == false).ForEach(b => b.SegName = "Mohammed");
            s.Stop();
            Console.WriteLine(String.Format("Time taken by LINQ foreach is {0} milliseconds", s.Elapsed));
            Console.WriteLine(String.Format("Time taken by LINQ foreach is {0} milliseconds", s.ElapsedTicks));

            s.Reset();
            s.Start();

            foreach (CNDSegment segment in cndSegmentList)
            {
                if (segment is PublicRecord == false)
                    segment.SegName = "Mohammed";
            }

            s.Stop();
            Console.WriteLine(String.Format("Time taken by normal foreach is {0} milliseconds", s.Elapsed));
            Console.WriteLine(String.Format("Time taken by normal foreach is {0} milliseconds", s.ElapsedTicks));


Time taken by LINQ foreach is 00:00:00.0002578 milliseconds
Time taken by LINQ foreach is 603 ElapsedTicks
Time taken by normal foreach is 00:00:00.0000059 milliseconds
Time taken by normal foreach is 14 ElapsedTicks

No comments:

Post a Comment