How to measure performance of C# method in milliseconds?
You can use JetBrains dotTrace to find a bottle neck methods for your application. So, how to measure a performance of a specific method? .NET framework since version 2.0 has Stopwatch class in System.Diagnostics namespace. You can use the following snippet to measure a performance of your method:
public static void DummyLoop()
{
for (int i = 0; i < 1000; i++)
{
System.Threading.Thread.Sleep(2);
}
}
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DummyLoop();
stopWatch.Stop();
double ms = (stopWatch.ElapsedTicks \* 1000.0) / Stopwatch.Frequency;
Console.WriteLine(string.Concat(ms.ToString(), " ms"));
Console.ReadLine();
}
And the output would be:
2929.66815614834 ms