So let’s say you have a few operations to run and it takes a while to finish the execution. You would like to know which step is taking so long and how to improve the performance of the application.

First, you could simply put a break point in each methods, but what if you want to be more precise and be able to give a report on the timing of the execution?

Create a timer or a StopWatch:

using System.Diagnostics;

// ...

Stopwatch sw = new Stopwatch();

sw.Start();

//Long task to measure
Thread.Sleep(3000);

sw.Stop();

Console.WriteLine("Elapsed={0}",sw.Elapsed);

This is really useful for log files.