C#內置提供給了測量代碼執行時長的方法,添加在你要檢測的代碼上,只需要句而已,代碼如下:
//初始化
Stopwatch stopWatch = new Stopwatch();
List<Tuple<DateTime, String>> temp = new List<Tuple<DateTime, string>>();
//開始測量
stopWatch.Start();
for (int i = 0; i < 100000; i++)
{
temp.Add(new Tuple<DateTime, string>(DateTime.Now,i.ToString()));
}
//測量結束
stopWatch.Stop();
//輸出
Console.WriteLine("Stopwatch總共花費{0}ms.", stopWatch.Elapsed.TotalMilliseconds);
是不是超級簡單?
不過,別忘了添加下面語句哦
using System.Diagnostics;
還有其他方式嗎?當然有啦,不過,我個人覺得你不需要掌握一萬種方法,只要把一個種方法用到爐火純青,就好。