Is there an C# attribute (for method) to calculate the time of execution of this method? Is there another way?
-
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
-
1Only 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 Answers
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.

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

- 1
- 1

- 3,782
- 3
- 24
- 42
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
- 1

- 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
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.

- 8,635
- 3
- 35
- 40
Something like this:
var oldTime = DateTime.Now;
//your code here
var resultTimeSpan = DateTime.Now - oldTime;

- 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