测量code的执行时间 [英] Measuring code execution time

查看:208
本文介绍了测量code的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道多少时间过程/函数/订单需要完成,用于测试目的。

这是我做的,但我的方法是错误的因为如果秒的差为0不能返回经过毫秒:

注意睡眠值为500毫秒所以经过秒是0,那么就不能返回毫秒。

 昏暗Execution_Start由于System.DateTime的= System.DateTime.Now
    Threading.Thread.Sleep(500)

    昏暗Execution_End由于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))
 

有人能告诉我一个更好的方式来做到这一点?也许有一个时间跨度

解决方法:

 昏暗Execution_Start作为新秒表
Execution_Start.Start()

Threading.Thread.Sleep(500)

的MessageBox.show(H:与&&Execution_Start.Elapsed.Hours安培; vbNewLine&安培; _
       男:&放大器; Execution_Start.Elapsed.Minutes和放大器; vbNewLine和放大器; _
       S:与& Execution_Start.Elapsed.Seconds和放大器; vbNewLine和放大器; _
       MS:与& Execution_Start.Elapsed.Milliseconds和放大器; vbNewLine,_
       code执行时间,MessageBoxButtons.OK,MessageBoxIcon.Information)
 

解决方案

一个更好的办法是使用的秒表,而不是的DateTime 的差异。

秒表类 - MSDN

  

提供一组方法和您可以使用属性   准确测量经过的时间。

 秒表计时秒表= Stopwatch.StartNew(); //创建并启动秒表实例
//你的样品code
System.Threading.Thread.Sleep(500);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
 

I want to know how much time a procedure/function/order takes to finish, for testing purposes.

This is what I did but my method is wrong 'cause if the difference of seconds is 0 can't return the elapsed milliseconds:

Notice the sleep value is 500 ms so elapsed seconds is 0 then it can't return milliseconds.

    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))

Can someone show me a better way to do this? Maybe with a TimeSpan?

The solution:

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)

解决方案

A better way would be to use Stopwatch, instead of DateTime differences.

Stopwatch Class - MSDN

Provides a set of methods and properties that you can use to accurately measure elapsed time.

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);

这篇关于测量code的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆