我想知道程序/函数/命令需要多少时间才能完成测试。测量代码执行时间
这是我做的,但我的方法是错误的因为如果秒的差为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)
@Soner如果我用C#标记它是因为欢迎C#代码。 – ElektroStudios 2013-05-04 16:14:00
你可以在这个问题上看到我的答案:> http://stackoverflow.com/a/16130243/1507182祝你好运 – Obama 2013-05-04 16:04:37
可能重复的[Find Execution time of a Method](http://stackoverflow.com/questions/16130232)/find-execution-time-of-a-method) – Jesse 2013-05-04 18:04:51