for/foreach/linq执行效率测试

本文通过测试LINQ、foreach、for及while循环在C#中的执行效率,对比不同循环方式查找特定元素的速度差异。测试中使用了一个包含一万个元素的整数列表,并进行了多次迭代以获取平均耗时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试代码
        private static void Test1(int count)
        {
            int s;
            List<int> list = new List<int>();
            for (int i = 0; i < 10000; i++)
            {
                list.Add(i);
            }
            Console.WriteLine("这是测试for/linq/foreach效率开始");
            DateTime d1 = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                var roleQuery = from r in list
                                where r == 3500
                                select r;
                foreach (var role in roleQuery)
                {
                    s = role;
                }
                Console.WriteLine("测试Linq第" + i +"次");
            }
            DateTime d2 = DateTime.Now;
            DateTime d3 = DateTime.Now;
            for (int c = 0; c < count; c++)
            {
                foreach (int role in list)
                {
                    if (role == 350)
                    {
                        s = role;
                    }
                }
                Console.WriteLine("测试foreach第" + c + "次");
            }
            DateTime d4 = DateTime.Now;
            DateTime d5 = DateTime.Now;
            for (int c = 0; c < count; c++)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i] == 350)
                    {
                        s = list[i];
                    }
                }
                Console.WriteLine("测试for第" + c + "次");
            }
            DateTime d6 = DateTime.Now;
            DateTime d7 = DateTime.Now;
            for (int c = 0; c < count; c++)
            {
                int i=0;
                while ( i<list.Count)
                {
                    if (list[i] == 350)
                    {
                        s = list[i];
                    }
                    i++;
                }
                Console.WriteLine("测试while第" + c + "次");
            }
            DateTime d8 = DateTime.Now;
            Console.WriteLine("linq测试时长为:" + (d2 - d1).ToString());
            Console.WriteLine("foreach测试时长为:" + (d4 - d3).ToString());
            Console.WriteLine("for测试时长为:" + (d6 - d5).ToString());
            Console.WriteLine("while测试时长为:" + (d8 - d7).ToString());
            Console.Read();
        }
 
调用代码
        static void Main(string[] args)
        {
            //NewMethod();
            int count = 100000000;
            Test1(count / 1000); //太大了时间太长
            //Test2(count);
        }
 
执行结果
18022348-fdf4d24db22648c4a3fa021fbfcb5311.png




转载于:https://www.cnblogs.com/weapon/archive/2013/05/18/3084801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值