博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
for/foreach/linq执行效率测试
阅读量:5891 次
发布时间:2019-06-19

本文共 1473 字,大约阅读时间需要 4 分钟。

测试代码
        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

你可能感兴趣的文章
Hyper-V 2016 系列教程30 机房温度远程监控方案
查看>>
笔记:认识.NET平台
查看>>
cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)
查看>>
gitlab 完整部署实例
查看>>
GNS关于IPS&ASA&PIX&Junos的配置
查看>>
影响企业信息化成败的几点因素
查看>>
SCCM 2016 配置管理系列(Part8)
查看>>
struts中的xwork源码下载地址
查看>>
ABP理论学习之仓储
查看>>
我的友情链接
查看>>
PHP 程序员的技术成长规划
查看>>
python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列
查看>>
js replace,正则截取字符串内容
查看>>
作业2
查看>>
nginx的信号量
查看>>
云im php,网易云IM
查看>>
开源 java CMS - FreeCMS2.3字典管理
查看>>
block,inline和inline-block概念和区别
查看>>
移动端常见随屏幕滑动顶部固定导航栏背景色透明度变化简单jquery特效
查看>>
javascript继承方式详解
查看>>