2

I hope with this question that we can assemble a bunch of good advices to measure performance of .Net applications.

Personally, I'm in a situation where I want to test an algorithm, and I want to be sure the measurements are reasonable precise. Therefore I do the following:

  • Release mode compiling (optimizations etc.)
  • Running the application outside Visual studio (avoids unnecessary checks etc.)
  • Looping over the code multiple times and take lowest time (makes sure it is JIT-compiled before and avoids periodic GC, OS specific stuff etc.) (*1)
  • Using a proper clock (*2)

(*1) If the input isn't always the same, it may make sense to do an average of the running time. Also, see Accurately measure elapsed wall clock time in .NET

So the question is really:

Is there anything I (or others) have missed in the above list? And elaborating of why the recommendations (in the list or new ones) works will be appreciated.

Update: Is anyone using ngen before benchmarking? If yes, why?

Thanks, Lasse Espeholt

Community
  • 1
  • 1
Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99
  • Since your goal is measuring, I think you're on the right track. For that purpose, I just use the run-it-N-times-and-divide-by-N method. If you also had the goal of locating points to optimize (time drains) I would recommend [stack-sampling](http://stackoverflow.com/questions/6328673/which-is-the-most-reliable-profiling-tool-gprof-or-kachegrind/6367165#6367165). – Mike Dunlavey Jun 18 '11 at 14:33

2 Answers2

1

These links may help..

Monitoring ASP.NET Application Performance

PerfMon – Your debugging buddy

Kon
  • 27,113
  • 11
  • 60
  • 86
1

If you are testing one algorithm, then you can also write a small test app (simple DIY solution), shove in a bunch of inputs, and run for one million iterations, then take the average. If you need to test multiple algorithms, then you can extend the small test app into a small test harness, and get results for all test algorithms. If you use Continuous Integration, then this test harness can give you frequent feedback on the performance of these algorithms.

Or, you use the profiling tools that come with Visual Studio team edition.

Regarding your #2 comment, the System.Diagnostics.Stopwatch class is about as good as it gets on a general-purpose OS such as windows.

Chris O
  • 5,017
  • 3
  • 35
  • 42