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开发者_StackOverflow社区 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.
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
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.
i think you should think about using code profiling... Use an external application like Ants Profiler to profile your code? Ants is from Red Gate and not free but i think that there are a some free/open source apps out there as well.
Some free/open source profilers here...
It's ok to stopwatch a method here and there but trying to do that for every method in your solution would quickly become unwieldy.
you could put the stopwatch class into an attribute used to decorate your methods, i.e. the AOP style. check out this
There are various timers. System.Timers.Timer
would be one such. But as the previous answer suggests, are you sure this is the right solutio to the problem you're actually trying to address?
精彩评论