C#确定方法的执行时间 [英] C# Determine execution Time of a method

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

问题描述

我想检查执行特定功能所需的时间,以便在程序中添加准确的时间延迟......

我之前尝试写过这个在我的职能之后:

I want to check the amount of time it takes for a specific function to be performed ,so that I will add accurate time delay on my program...
I tried to write this before and after my function:

public static String GetTimestamp(this DateTime value)
      {
          return value.ToString(mmssffff);
      }





然后我计算了两个结果之间的差异,我得到350.I不知道它是微秒还是毫秒...



你知道结果的意义吗?(350)

你还有另外一个想法吗?

谢谢!



Then I calculated the difference between both results, and I get 350.I dont know if it microseconds or milliseconds...

Do you know the meaning of the result?(350)
Do you have another idea to do this?
Thanks!

推荐答案

为什么不写这样的东西:



Why don't you write something like this:

DateTime start = DateTime.Now; //Start time
DoFoo(); //Execute your method
DateTime end = DateTime.Now;   //End time

TimeSpan timeDifference = end - start; //Time span between start and end = the time span needed to execute your method
int difference_Miliseconds = (int)span.TotalMilliseconds; //Gives you the time needed to execute the method in miliseconds (= time between start and end in miliseconds)


解决方案1中显示的方法不是最准确的。最准确的时间由类 System.Diagnostics.Stopwatch 提供:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs。 110%29.aspx [ ^ ]。



您可以通过检查这两个静态字段找出时间分辨率:

http://msdn.microsoft .com / zh-cn / library / system.diagnostics.stopwatch.frequency(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(V = vs.110)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。



-SA
The method shown in Solution 1 is not the most accurate. The most accurate timing is provided by the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[^].

You can find out time resolution provided by checking these two static fields:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(v=vs.110).aspx[^].

—SA


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

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