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 = (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();