7

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

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • 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 '11 at 13:25
  • 1
    Only if you use an attribute based code rewriter such as postsharp. – CodesInChaos May 13 '11 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 '11 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 '11 at 13:33
  • Bastien have you found any solution to create attribute for calculating execution time? – Hien Nguyen Feb 27 '19 at 02:16

6 Answers6

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.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
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?

Community
  • 1
  • 1
Bek Raupov
  • 3,782
  • 3
  • 24
  • 42
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.

Community
  • 1
  • 1
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • yeah, the preferrable form is going the fix the fact that you aren't using a proper profiler :_) **[see FAQ entry no. 5](http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers)** – sehe May 13 '11 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.

Russell Troywest
  • 8,635
  • 3
  • 35
  • 40
1

Simple as pie:

  1. DON'T
  2. Use a profiler
  3. For production measurements use Performance Counters
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
-3

Something like this:

  var oldTime = DateTime.Now;
  //your code here
  var resultTimeSpan = DateTime.Now - oldTime;
Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • Don't use DateTime for time measurement, use Stopwatch - also see this: https://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events – Leon May 20 '19 at 13:16