效能分析 之 統計代碼的執行時間

來源:互聯網
上載者:User

標籤:代碼   lin   ons   tostring   timer   cut   set   i++   統計代碼   

一、System.Diagnostics 命名空間下的 Stopwatch 類
//執行個體化一個計時器Stopwatch watch = new Stopwatch();//開始計時watch.Start();//此處為要計算的運行代碼for (int i = 1; i < 1000000; i++) { }   // Execute the task to be timed//結束計時watch.Stop();//擷取當前執行個體測量得出的總已耗用時間(以毫秒為單位)string time = watch.ElapsedMilliseconds.ToString();//說明:Stopwatch提供了幾個方法用以控制Stopwatch對象。Start方法開始一個計時操作,Stop方法停止計時。為避免這種情況,在第二次計時前用Reset方法將對象歸零 Console.WriteLine("Elapsed: {0}",watch.Elapsed);Console.WriteLine("In milliseconds: {0}",watch.ElapsedMilliseconds);Console.WriteLine("In timer ticks: {0}",watch.ElapsedTicks);
二、DateTime 類
DateTime startTime = DateTime.Now;Console.WriteLine ("Started: {0}", startTime); // Execute the task to be timedfor (int i=1; i < 100000; i++){}  DateTime stopTime = DateTime.Now;Console.WriteLine ("Stopped: {0}", stopTime);TimeSpan elapsedTime = stopTime - startTime;Console.WriteLine ("Elapsed: {0}", elapsedTime);Console.WriteLine ("in hours       :" + elapsedTime.TotalHours);Console.WriteLine ("in minutes     :" + elapsedTime.TotalMinutes);Console.WriteLine ("in seconds     :" + elapsedTime.TotalSeconds);Console.WriteLine ("in milliseconds:" + elapsedTime.TotalMilliseconds);

 

效能分析 之 統計代碼的執行時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.