码迷,mamicode.com
首页 > Windows程序 > 详细

C#获取程序代码执行时长

时间:2017-07-24 00:05:20      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:arp   watch   ons   new   read   代码   dmi   c#   sharp   

            ArrayList list = new ArrayList();
            long startTicks = DateTime.Now.Ticks;
            for (int i = 0; i < 1000000; i++)
            {
                list.Add(i);
            }
            for (int i = 0; i < 1000000; i++)
            {
                int value = (int)list[i];
            }
            long endTicks = DateTime.Now.Ticks;
            Console.WriteLine("arrayList执行时长:" + (endTicks-startTicks));


             List<int> list2 = new  List<int>();
            long startTicks1 = DateTime.Now.Ticks;
            for (int i = 0; i < 1000000; i++)
            {
                list2.Add(i);
            }
            for (int i = 0; i < 1000000; i++)
            {
                int value = (int)list2[i];
            }
            long endTicks1 = DateTime.Now.Ticks;
            Console.WriteLine("List<int>执行时长:" + ( endTicks1-startTicks1 ));

            Console.WriteLine("\n-------------------------------------------------------\n");
            //方法二
            //using System.Diagnostics;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 9000000; i++)
            {
                list.Add(i);
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed); //timespan
            Console.WriteLine(stopwatch.ElapsedMilliseconds); //毫秒
            Console.WriteLine(stopwatch.ElapsedTicks);


            Console.ReadKey();

  

C#获取程序代码执行时长

标签:arp   watch   ons   new   read   代码   dmi   c#   sharp   

原文地址:http://www.cnblogs.com/wxylog/p/7226297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!