In this tutorial i'll show you how to calculate the excution time of a method. It's very useful to help you know how much time your code excution.

We will use Stopwatch class to play demo. Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        //Begin calculate excution time
        for (int i = 0; i < 1000000; i++)
            Console.Write(i);
        stopWatch.Stop();
        //Get the elapsed time
        TimeSpan ts = stopWatch.Elapsed;
        Console.WriteLine($"Execution Time: {ts.TotalMilliseconds}");
    }
}

A Stopwatch instance can measure elapsed time for one interval or the total of elapsed time across multiple intervals.

You can use Start to begin measuring elapsed time and use Stop to stop measuring elapsed time