Today I was asked to find the best way of measuring execution time of DoSomeWork() method.
MSDN gives the pretty fine answer for this, but google gives too much links with using DateTime.Now to measure execution time.
System.Environment.TickCount and the System.Diagnostics.Stopwatch class are two that work well for finer resolution and straightforward usage.

Stopwatch Class provides a set of methods and properties that you can use to accurately measure elapsed time.

See MSDN:

Example code:

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Stopwatch stopWatch = new Stopwatch();
  9.         stopWatch.Start();
  10.         DoSomeWork();
  11.         stopWatch.Stop();
  12.         // Get the elapsed time as a TimeSpan value.
  13.         TimeSpan ts = stopWatch.Elapsed;
  14.         // Format and display the TimeSpan value.
  15.         string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”,
  16.             ts.Hours, ts.Minutes, ts.Seconds,
  17.             ts.Milliseconds / 10);
  18.         Console.WriteLine(elapsedTime, “RunTime”);
  19.     }
  20.     static void DoSomeWork()
  21.     {
  22.       // code does hard work here :)
  23.     }
  24. }

If you haven't subsribed yet, you can subsribe below: