2013-05-04 73 views
69

我想知道程序/函數/命令需要多少時間才能完成測試。測量代碼執行時間

這是我做的,但我的方法是錯誤的因爲如果秒的差爲0不能返回經過毫秒:

注意睡眠值爲500毫秒所以經過的秒數爲0,那麼它可以不會返回毫秒。

Dim Execution_Start As System.DateTime = System.DateTime.Now 
    Threading.Thread.Sleep(500) 

    Dim Execution_End As System.DateTime = System.DateTime.Now 
    MsgBox(String.Format("H:{0} M:{1} S:{2} MS:{3}", _ 
    DateDiff(DateInterval.Hour, Execution_Start, Execution_End), _ 
    DateDiff(DateInterval.Minute, Execution_Start, Execution_End), _ 
    DateDiff(DateInterval.Second, Execution_Start, Execution_End), _ 
    DateDiff(DateInterval.Second, Execution_Start, Execution_End) * 60)) 

有人可以告訴我一個更好的方法來做到這一點嗎?也許用TimeSpan

解決辦法:

Dim Execution_Start As New Stopwatch 
Execution_Start.Start() 

Threading.Thread.Sleep(500) 

MessageBox.Show("H:" & Execution_Start.Elapsed.Hours & vbNewLine & _ 
     "M:" & Execution_Start.Elapsed.Minutes & vbNewLine & _ 
     "S:" & Execution_Start.Elapsed.Seconds & vbNewLine & _ 
     "MS:" & Execution_Start.Elapsed.Milliseconds & vbNewLine, _ 
     "Code execution time", MessageBoxButtons.OK, MessageBoxIcon.Information) 
+0

@Soner如果我用C#標記它是因爲歡迎C#代碼。 – ElektroStudios 2013-05-04 16:14:00

+0

你可以在這個問題上看到我的答案:> http://stackoverflow.com/a/16130243/1507182祝你好運 – Obama 2013-05-04 16:04:37

+1

可能重複的[Find Execution time of a Method](http://stackoverflow.com/questions/16130232)/find-execution-time-of-a-method) – Jesse 2013-05-04 18:04:51

回答

152

一個更好的辦法是使用Stopwatch,而不是DateTime差異。

Stopwatch Class - MSDN

提供一組,你可以用它來 準確地測量經過時間的方法和屬性。

Stopwatch stopwatch = Stopwatch.StartNew(); //creates and start the instance of Stopwatch 
//your sample code 
System.Threading.Thread.Sleep(500); 
stopwatch.Stop(); 
Console.WriteLine(stopwatch.ElapsedMilliseconds); 
+0

@ElektroHacker,歡迎您,其它更容易使用,加上它比DateTime更準確:) – Habib 2013-05-04 16:06:27

+0

請參閱https://github.com/chai-deshpande/LogExec(也可以使用NUGET軟件包https://www.nuget.org/packages/LogExec/ )。這與@ soner-gonul提到的完全相同 - 但是,這種用法是免費的,並隱藏了所有樣板代碼。當你想非常頻繁地使用它時非常有用。它還使用Common.Logging,以便您可以與您的首選日誌記錄提供程序集成。 – Chai 2014-01-20 04:11:10

+0

@Habib你能幫我理解爲什麼Thread.sleep(500)是必須的嗎?不能跳過睡眠,會不會使效率降低? – 2017-03-19 12:43:59

45

Stopwatch措施的時間過去了。

// Create new stopwatch 
Stopwatch stopwatch = new Stopwatch(); 

// Begin timing 
stopwatch.Start(); 

Threading.Thread.Sleep(500) 

// Stop timing 
stopwatch.Stop(); 

Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); 

這是DEMO

+0

它可以用於每行代碼的執行時間嗎?例如: bindingSource.datasource = db.table; //需要多長時間? – vaheeds 2013-07-25 06:44:39

+2

@vaheeds當然。只需在每條線的頂端啓動這個「秒錶」,並在每條線後停下。請記住,您需要在每一行之後使用['Stopwatch.Reset'](http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch.reset.aspx)來計算。根據你的路線;看看http://ideone.com/DjR6ba – 2013-07-25 06:48:04

+0

我做了一個小技巧,以獲得毫秒圖片。這樣watch.Elapsed.ToString()。Split('。')[0] – Doruk 2016-09-07 07:50:18

8

如果使用秒錶類,可以使用.StartNew()方法重置手錶爲0。所以,你不必叫.reset段()其次。開始() 。可能會派上用場。

21

您可以使用此秒錶包裝:

public class Benchmark : IDisposable 
{ 
    private readonly Stopwatch timer = new Stopwatch(); 
    private readonly string benchmarkName; 

    public Benchmark(string benchmarkName) 
    { 
     this.benchmarkName = benchmarkName; 
     timer.Start(); 
    } 

    public void Dispose() 
    { 
     timer.Stop(); 
     Console.WriteLine($"{benchmarkName} {timer.Elapsed}"); 
    } 
} 

用法:

using (var bench = new Benchmark($"Insert {n} records:")) 
{ 
    ... your code here 
} 

輸出:

Insert 10 records: 00:00:00.0617594 

對於高級場景,您可以使用Benchmark.ItNBench

+2

謝謝,迄今爲止的最佳答案 – pixel 2016-06-16 21:44:26

+1

謝謝,正在集中尋找。 ActionFilter也完成了這個策略。 – 2016-11-14 10:22:50

1

秒錶專爲此目的而設計,並且是測量.NET中時間執行的最佳方法之一。

var watch = System.Diagnostics.Stopwatch.StartNew(); 
/* the code that you want to measure comes here */ 
watch.Stop(); 
var elapsedMs = watch.ElapsedMilliseconds; 

不要使用DateTimes來測量.NET中的時間執行。