7

Is there an C# attribute (for method) to calculate the time of execution of this method? Is there another way?

5
  • This question makes no sense. Attributes only work when something is looking for them, which means you need some code to check for an attribute and then do something. Also this one is what?
    – Tejs
    May 13, 2011 at 13:25
  • 1
    Only if you use an attribute based code rewriter such as postsharp. May 13, 2011 at 13:28
  • 2
    @Tejs It may make no sense because the OP likely has a misunderstanding of what attributes are and how to use them. In which case, you correct him and help him rather than give him a downvote. And "this one" is obviously referring to the method.
    – Phil
    May 13, 2011 at 13:32
  • I downvoted the question because he never specified what this method was. For example, if it was an action method in MVC, there are established ways of doing that. If its a random method in a console app, the solution is different.
    – Tejs
    May 13, 2011 at 13:33
  • Bastien have you found any solution to create attribute for calculating execution time? Feb 27, 2019 at 2:16

6 Answers 6

6

You can use Aspect Oriented programming to do this. Look at this example from PostSharp: http://www.sharpcrafters.com/solutions/performance

The trick here is to define behavior that will be injected into your code at compile time. One use case is to add timing to methods like you need.

5

there is other way: you could use VS Profiler to check how many times and how long particuler method is executed.

or you could 3rd party library which uses Attributes for profiling the performance, see this related topic What Are Some Good .NET Profilers?

4

You could use a Timer, start it before the function call and then stop it after the method is complete. Then output the time in a preferable form!

OR

If you don't want to write the manual code, check out Visual Studio Profilers or SO - Best Profilers.

1
  • yeah, the preferrable form is going the fix the fact that you aren't using a proper profiler :_) see FAQ entry no. 5
    – sehe
    May 13, 2011 at 13:35
3

If you are wanting to write some test code to profile various functions or parts thereof you would use a System.Diagnostics.Stopwatch to keep track of elapsed time. Like this:

public void DoSomething(){
  Stopwatch stopWatch = new Stopwatch();
  stopWatch.Start();

  //stuff you want to time

  stopWatch.Stop();

  System.Console.Writeln(String.Format("Total Ticks: {0}", stopWatch.ElapsedTicks))
}

If it's a more general thing you want to do (ie work out what parts of a program are slow) then use a profiler like some of the other answers suggest.

1

Simple as pie:

  1. DON'T
  2. Use a profiler
  3. For production measurements use Performance Counters
-3

Something like this:

  var oldTime = DateTime.Now;
  //your code here
  var resultTimeSpan = DateTime.Now - oldTime;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.