close

.Net Framework要計算程式執行時間

真的是還滿簡單的啦

System.Diagnostics.Stopwatch大概是最常用的方式吧!!

方法1:

Dim sw As System.Diagnostics.Stopwatch = New System.Diagnostics.Stopwatch()
sw.Reset()
sw.Start()
'計算程式執行時間--Start

'要執行的程式 
Dim strVal As String = ""
if x = y then
strVal = "我跑的很快!!"
end if
System.Threading.Thread.Sleep(1000)

'計算程式執行時間--Stop
sw.Stop()
sw.Elapsed.TotalMilliseconds.ToString()
Dim ts As System.TimeSpan = sw.Elapsed
Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)

方法2:

Dim dtstart As DateTime = DateTime.Now
'計算程式執行時間--Start

'執行的程式
Dim strVal As String = ""
if x = y then
strVal = "我跑的很快!!"
end if
System.Threading.Thread.Sleep(1000)

'計算程式執行時間--Stop
Dim ts2 = DateTime.Now - dtstart
Dim elapsedTime2 As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts2.Hours, ts2.Minutes, ts2.Seconds, ts2.Milliseconds / 10)

參考:

http://www.dotblogs.com.tw/larrynung/archive/2010/04/03/14384.aspx

http://blog.xuite.net/tolarku/blog/39275301

arrow
arrow
    全站熱搜

    鴨爸 發表在 痞客邦 留言(0) 人氣()