首页 > 代码库 > C#获取程序代码执行时长

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

            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 = http://www.mamicode.com/(int)list[i];"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 = http://www.mamicode.com/(int)list2[i];"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#获取程序代码执行时长