6

I need to do some performance benchmarks on .NET programs (C#) in Windows, but I haven't done benchmarking much in the Windows world. I've looked into using the Windows 2000/XP Performance monitor with custom counters for this, but I don't think this is quite what I want.

Are there any good system facilities for this in Windows XP, or do I need to just use System.Diagnostics.Stopwatch [edit] and write text logs for manual interpretation, or is there something else?

Edit: is there anything beyond System.Diagnostics.Stopwatch?

Ben Collins
  • 19,889
  • 16
  • 119
  • 182

6 Answers6

8
using System.Diagnostics;
....

Stopwatch sw = new Stopwatch();

sw.Start();

// Code you want to time...

// Note: for averaged accuracy (without other OS effects), 
//       run timed code multiple times in a loop 
//       and then divide by the number of runs.

sw.Stop();

Console.WriteLine("Took " + sw.ElapsedTicks + " Ticks");
Mitch Wheat
  • 280,588
  • 41
  • 444
  • 526
  • You should also separate the first run from the averaged results as it may be highly skewed due to the JIT compilation process. – Ed S. Jun 05 '11 at 05:17
  • Ed. S: Good point, but not necessarily. If the code only runs once in reality, then the JIT time probably needs to be factored in. – Mitch Wheat Jun 05 '11 at 05:21
  • 1
    Sure, I didn't mean to disregard that info altogether, just to make sure to segregate it out so that it doesn't boost the average up. As long as the person doing the test is aware of this fact it should be fine. I was tweaking an XML parsing routine that can be called on some pretty large chunks of data (relative to what is typical in my app anyway) and the first pass took ~931ms, while all subsequent calls took ~91ms. – Ed S. Jun 05 '11 at 05:28
  • What does one tick correspond to? – Harshit Jain May 06 '15 at 18:10
5

For micro-benchmarking I really like MeasureIt (can be downloaded from http://msdn.microsoft.com/en-us/magazine/cc500596.aspx). It is a test project written by Vance Morrison a Performance Architect on the CLR. It currently has a good set of benchmarks for a number of .Net/CLR core methods. The best part of it is that it is trivial to tweak and add new benchmarks for whatever you would like to test. Simply run "MeasureIt /edit" and it will launch VS with the project for itself so that you can view how those benchmarks are written and add new ones in a similar fashion if you like.

As already been stated StopWatch is probably the easiest way to do this and MeasureIt uses StopWatch underneath for its timings but it also does some other things like running a block of code X times and then providing you stats for the runs and what not.

Wes Haggard
  • 4,086
  • 1
  • 19
  • 21
4

There are quite a few profilers out there. Here are some of the ones I know of:

If you go on with using System.Diagnostics.Stopwatch, you will be able to instrument and measure only particular points of your code, where you explicitly put the Start/Stop around. This is good enough for measuring specific pieces, like a tight loop or things like that, but it's not going to give you a complete picture of where your program spends most of its time.

Aliaksandr Belik
  • 11,827
  • 6
  • 58
  • 86
Franci Penov
  • 71,783
  • 15
  • 124
  • 160
4

If you want just some quick numbers, you can use Powershell to time overall execution. Use the measure-command cmdlet. It's roughly equivalent to "time" in Unix.

> measure-command { your.exe arg1 }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 4
Milliseconds      : 996
Ticks             : 49963029
TotalDays         : 5.78275798611111E-05
TotalHours        : 0.00138786191666667
TotalMinutes      : 0.083271715
TotalSeconds      : 4.9963029
TotalMilliseconds : 4996.3029
Justin R.
  • 21,845
  • 21
  • 102
  • 147
3

This may not be what you want, but dotTrace offers many useful diagnostics and is integrated into Visual Studio.

user23126
  • 2,001
  • 5
  • 18
  • 15
1

If your project is quite big and there are lots of modules doing big number of calls you can your: http://www.moduleanalyzer.com/

Pablo Yabo
  • 2,183
  • 2
  • 21
  • 29