System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //開始監視代碼運行時間 //須要測試的代碼 watch.Stop(); //中止監視 TimeSpan timespan = watch.Elapsed; //獲取當前實例測量得出的總時間 System.Diagnostics.Debug.WriteLine("打開窗口代碼執行時間:{0}(毫秒)", timespan.TotalMilliseconds); //總毫秒數