Stopwatch is an excellent class in the System.Diagnostics namespace that allows a simple execution time measurement. It is extremely accurate and handy when other, more complex monitoring tools are not available or are just not needed for the sake of simplicity.
This built-in class was introduced in .Net framework 2.0 and is suited for several scenarios. It helps you to optimize your code, to choose among several possible solutions or to determine a performance spike.
Now we're going to take a closer look at how to use Stopwatch properly.
Stopwatch in action
Before we can even start using this class in our solution, we need to add the corresponding namespace.
- using System.Diagnostics;
- Stopwatch stopwatch = new Stopwatch();
Now, everything is set and we can start measuring the time. We start the measurement by calling the Start method that internally saves the current time. To stop it, we call the Stop method. Of course, there should be some long-lasting action between these two that we want to monitor.
- // Begin timing
- stopwatch.Start();
- // Do something
- // Long running process
- // Stop timing
- stopwatch.Stop();
- // Write result
- Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
- // Write result
- Console.WriteLine("Time elapsed: {0}", stopwatch.ElapsedMilliseconds);
- // Write result
- Console.WriteLine("Time elapsed: {0}", stopwatch.ElapsedTicks);
- var stopwatch = Stopwatch.StartNew();
Summary
Stopwatch is an easy to use tool that measures the execution time of a desired process/action. It helps to optimize the code in scenarios where the time plays an important role.

Manish Kumar ChoudharyPosted Jan 12, 2015, 10:22 AM
Nice one.. sir how to measure the performance of client side java script jquery etc code?
Humayun Kabir MamunPosted Jan 12, 2015, 3:37 AM
Nice...
Vithal WadjePosted Jan 12, 2015, 2:02 AM
nice
Guest UserPosted Jan 12, 2015, 1:47 AM
Hi Michal, in VS2015 while executing program in debug mode, you can see time taken & CPU usage after each step. Just thought to share with you