Measure execution time in C#
I want to measure the execution of a piece of code and I'm wondering what the best method to do this is? Option 1: DateTime StartTime = DateTime.Now; //Code TimeSpan ts = DateTime.Now.Subtract(StartTime); string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine(elapsedTime, "RunTime"); Option 2: using System.Diagnostics; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //Code stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the