C#2.0执行时间计时器

J.H*_*rix 5 c# performance timer stopwatch execution-time

我希望能够计算执行许多函数所需的时间.我在考虑使用某种类型的秒表课程.我可以在每个函数调用之前和之后调用start/stop,但这对我来说似乎非常难看.没有秒表课,有没有办法做到这一点?Reflections类中的某些东西可以帮助解决这个问题吗?

在此先感谢您的投入.

Mar*_*eli 3

我认为你必须尝试 PostSharp 方式http://www.postsharp.org/

更改示例中的代码:

public class ProfileMethodsAttribute : OnMethodBoundaryAspect 
{ 
  public override void OnEntry( MethodExecutionEventArgs eventArgs) 
  { eventArgs.MethodExecutionTag = Stopwatch.StartNew();  } 

  public override void OnExit( MethodExecutionEventArgs eventArgs) 
  { 
     // Here u can get the time
     Console.WriteLine(((Stopwatch)eventArgs.MethodExecutionTag).ElapsedMilliseconds);
  } 
}
Run Code Online (Sandbox Code Playgroud)

您可以通过任何方式获得休息时间