Using Stopwatch in C#

The Stopwatch object is often used to (as you do here), measure how long things take. One quick thing to remember here is that it will take the time for everything you do between starting and stopping it, so make sure you only put the actual code you want to time between those.

using System.Diagnostics;

//...
void StopwatchUsingMethod()
{
  //A: Setup and stuff you don't want timed
  var timer = new Stopwatch();
  timer.Start();

  //B: Run stuff you want timed
  timer.Stop();

  TimeSpan timeTaken = timer.Elapsed;
  string foo = "Time taken: " + timeTaken.ToString(@"m\:ss\.fff"); 
}

Foo will here show the minutes, seconds, and milliseconds it took to complete, though will show as wrong if it takes more than 59 minutes. Further information about TimeSpan can be found here: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings

If I understand your code correctly, you want to put everything up to and including the first sdk.Display(); line in the A section and only put the call to Solve in the B section, so something like

static void Main(string[] args)
{
  //...
  sdk.Display();
  var timer = new Stopwatch();

  timer.Start();
  sdk.Solve(sdk.NextAvailableCell(), new Point());
  timer.Stop();

  Console.WriteLine("\n\n\nSolved Grid"); 
  //...you can use the time taken here
}

Tags:

C#

Stopwatch