1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //目的: 6 // 比较ArrayList和List<T>的性能 7 8 //实例化Stopwatch 9 Stopwatch sw = new Stopwatch(); 10 ArrayList al = new ArrayList(); 11 List<int> mylist = new List<int>(); 12 13 //开始计时 14 sw.Start(); 15 for (int i = 0; i < 10000000; i++) 16 { 17 al.Add(i); 18 } 19 //停止计时 20 sw.Stop(); 21 //获取运行时间 22 Console.WriteLine(sw.Elapsed.ToString()); 23 24 //重置时间并开始计时 25 sw.Restart(); 26 for (int i = 0; i < 10000000; i++) 27 { 28 29 } 30 //停止计时 31 sw.Stop(); 32 //获取运行时间 33 Console.WriteLine(sw.Elapsed.ToString()); 34 } 35 }