Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 2.0 Execution Time Timer

I would like to be able to calculate the amount of time a number of functions take to execute. I was thinking about using some type of stopwatch class. I can call start/stop before and after each function call, however that seems horribly ugly to me. Is there a way to do this without a stopwatch class? Can something in the Reflections class help with this?

Thanks in advance for you input.

like image 405
J.Hendrix Avatar asked Jan 06 '10 15:01

J.Hendrix


2 Answers

I think u must try the PostSharp way http://www.postsharp.org/

Changing the code in the sample for this one:

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

You can get the time off any method

like image 129
Marcos Meli Avatar answered Sep 21 '22 22:09

Marcos Meli


I recommend the sponsor of the performance tag ... Red Gate Ants. You can get a demo that will run your app and give you performance information for every line and every method that your app hits while it is being profiled.

like image 24
Jake Pearson Avatar answered Sep 18 '22 22:09

Jake Pearson