1. 程式人生 > >C#測試程式碼段效能耗時

C#測試程式碼段效能耗時

一:

            DateTime BeginTime = System.DateTime.Now;
            //程式碼
            DateTime EndTime = System.DateTime.Now;
            TimeSpan ts = EndTime.Subtract(BeginTime);
            MessageBox.Show(ts.TotalMilliseconds.ToString());

 

二:

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Reset();
            watch.Start();
            //程式碼
            watch.Stop();
            MessageBox.Show(watch.ElapsedMilliseconds.ToString());

以上轉自別人新浪部落格,非原創