C# foreach,linq,delegate集合查询的性能比较

来源:岁月联盟 编辑:exp 时间:2011-11-04

 

昨天和别人讨论了一下linq的性能,自我觉得linq的性能不行,但是实际上linq还是在性能上有过人之处的,linq to sql除外,以下是简单的性能测试比较代码,在这里解释一下,代码的执行速度计时测试不能用datetime了,我还是个小白,刚开始用的datetime,结果发现linq的性能并不行,但是用StopWatch才发现了事实,以前对linq的偏见的同学还是拥抱一下linq吧,也许某些方面还存在没完全理解透,还请园友们给予批评指正。

    class Program

    {

        static void Main(string[] args)

        {

            test();

        }

 

       static void test()

        {

            List<MyClass> list1 = new List<MyClass>();

            for (int i = 0; i < 10000000; i++)

            {

                MyClass aa=new MyClass();

                aa.Name = "测试数据" + i;

                aa.id = i;

                list1.Add(aa);

            }

 

            Stopwatch timer = new Stopwatch();

 

 

            #region for循环

            timer.Start();

            List<MyClass> list2 = new List<MyClass>();

 

            foreach (MyClass s in list1)

            {

                if (s.id >= 52 && s.id < 850) { list2.Add(s); }

            }

            timer.Stop();

 

            Console.Write("集合匹配数" + list2.Count + ",for循环耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

 

 

            #region linq

            timer = new Stopwatch();

            timer.Start();

            var list3 = list1.Where(product => product.id >= 52 && product.id < 850);

            timer.Stop();

 

 

            Console.Write("集合匹配数" + list3.Count() + ",linq耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

 

 

            #region delegate

            timer = new Stopwatch();

            timer.Start();

            List<MyClass> list4 = list1.FindAll(delegate(MyClass post)

            {

                return post.id >= 52 && post.id < 850;

            });

            timer.Stop();

 

            Console.Write("集合匹配数" + list4.Count() + ",delegate耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

 

 

            Console.Read(); 

        }

 

        public class MyClass

        {

            public string Name { get; set; }

            public int id { get; set; }

        }

    }

测试的效果截图如下:

 

/

 

如果理解的不正确,还请大家批评一下,不能让错误的思想误人子弟

 

根据园友的批评指正,重新整理了一下代码,虽然是个简单的东西,但是对于新手批评比装懂好,对园友的细心指正不胜感激。

重新整理的代码如下:

  View Code

  class Program

    {

        static void Main(string[] args)

        {

            test();

        }

 

       static void test()

        {

            List<MyClass> list1 = new List<MyClass>();

            for (int i = 0; i < 10000000; i++)

            {

                MyClass aa=new MyClass();

                aa.Name = "测试数据" + i;

                aa.id = i;

                list1.Add(aa);

            }

 

            Stopwatch timer = new Stopwatch();

 

 

            #region for循环

            timer.Start();

            List<MyClass> list2 = new List<MyClass>();

 

            int count = 0;

            foreach (MyClass s in list1)

            {

                if (s.id >= 52 && s.id < 850) { list2.Add(s); }

            }

            count = list2.Count;

            timer.Stop();

 

            Console.Write("集合匹配数" + count + ",for循环耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

 

 

            #region linq

            timer = new Stopwatch();

            timer.Start();

            var list3 = list1.Where(product => product.id >= 52 && product.id < 850);

            //list1.Where(product => product.id >= 52 && product.id < 850).ToArray().Count();

            int count3 = list3.Count();

            timer.Stop();

 

 

            Console.Write("集合匹配数" + count3 + ",linq耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

 

 

            #region delegate

            timer = new Stopwatch();

            timer.Start();

            List<MyClass> list4 = list1.FindAll(delegate(MyClass post)

            {

                return post.id >= 52 && post.id < 850;

            });

            int count4 = list4.Count();

            timer.Stop();

 

            Console.Write("集合匹配数" + count4 + ",delegate耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

 

 

            Console.Read(); 

        }

 

        public class MyClass

        {

            public string Name { get; set; }

            public int id { get; set; }

        }

    }

测试截图

 

/

 

第三次更新代码,这次没有先后顺序产生的问题了:

  View Code

    class Program

    {

       static void Main(string[] args)

        {

            List<MyClass> list1 = new List<MyClass>();

            for (int i = 0; i < 10000000; i++)

            {

                MyClass aa = new MyClass();

                aa.Name = "测试数据" + i;

                aa.id = i;

                list1.Add(aa);

            }

 

            test_linq(list1);

 

            test_foreach(list1);

 

            test_delegate(list1);

 

          

            Console.Read();

        }

 

       static void test_foreach(List<MyClass> list1)

        {

            Stopwatch timer = new Stopwatch();

            #region foreach循环

            timer.Start();

            List<MyClass> list2 = new List<MyClass>();

 

            int count = 0;

            foreach (MyClass s in list1)

            {

                if (s.id >= 52 && s.id < 850) { list2.Add(s); }

            }

            count = list2.Count;

            timer.Stop();

 

            Console.Write("集合匹配数" + count + ",for循环耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

        }

       static void test_linq(List<MyClass> list1)

        {

            Stopwatch timer = new Stopwatch();

            #region linq

            timer = new Stopwatch();

            timer.Start();

            var list3 = list1.Where(product => product.id >= 52 && product.id < 850);

            //list1.Where(product => product.id >= 52 && product.id < 850).ToArray().Count();

            int count3 = list3.Count();

            timer.Stop();

 

 

            Console.Write("集合匹配数" + count3 + ",linq耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

        }

       static void test_delegate(List<MyClass> list1)

        {

            Stopwatch timer = new Stopwatch();

            #region delegate

            timer = new Stopwatch();

            timer.Start();

            List<MyClass> list4 = list1.FindAll(delegate(MyClass post)

            {

                return post.id >= 52 && post.id < 850;

            });

            int count4 = list4.Count();

            timer.Stop();

 

            Console.Write("集合匹配数" + count4 + ",delegate耗时:");

            Console.WriteLine(timer.Elapsed.Ticks);

            #endregion

        }

 

        public class MyClass

        {

            public string Name { get; set; }

            public int id { get; set; }

        }

    }

 

 

/

 

/

 

看以上截图基本没什么大的变化

总结:没有认识linq在延时加载方面的处理,所以linq的count操作触发了linq的实际查询操作,也许还没有深入理解,但是总有一天会理解的

 

博客地址:http://www.jqpress.com/post/43.aspx