2013-05-04 76 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中的时间执行。