What is the best way to measure the execution time of a function?

Obviously, I can do DateTime.Now.After - DateTime.Now.Before , but there must be something more complicated.

Any advice appreciated.

+82
performance c # benchmarking execution-time
Dec 19 '08 at 13:10
source share
4 answers

The System.Environment.TickCount and System.Diagnostics.Stopwatch class are two that work well for finer resolution and easier use.

See also:

  • Is DateTime.Now the best way to measure the performance of functions?
  • .NET High Resolution Timer
  • Environment.TickCount vs DateTime.Now
  • What is the best way to test programs on Windows?
+95
Dec 19 '08 at 13:12
source share

I would definitely advise you to take a look at System.Diagnostics.Stopwatch

And when I looked around more about the stopwatch, I found this site;

Beware of the stopwatch

Another possibility was mentioned there.

Process.TotalProcessorTime

+44
Dec 19 '08 at 13:14
source share

Use profiler

However, your approach will work, but if you are looking for more complex approaches. I would suggest using C # Profiler .

The benefits they have:

  • You can even get the level of applications Decay
  • No changes are required in your codebase.
  • Toolkit usually has very low costs, so you can get very accurate results.

There are many open-source available .

+11
Dec 19 '08 at 13:18
source share

Tickcount is good, but I suggest running it 100 or 1000 times and calculating the average. This not only makes it more measurable - in the case of really fast / short functions, but helps to cope with some one-time effects caused by overhead.

+4
Dec 19 '08 at 13:16
source share



All Articles