Calculate the execution time of a method

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main()
    {
       // Create new stopwatch
       Stopwatch stopwatch = new Stopwatch();

      // Begin timing
      stopwatch.Start();

      // Do something
      for (int i = 0; i < 1000; i++)
      {
        Thread.Sleep(1);
      }

      // Stop timing
      stopwatch.Stop();

      // Write result
      Console.WriteLine("Time elapsed: {0}",
          stopwatch.Elapsed);
    }
}

Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment