1. 程式人生 > >C#獲取程序代碼執行時長

C#獲取程序代碼執行時長

using csharp edt lis blog 程序 end 程序代碼 mil

            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#獲取程序代碼執行時長