Question

There are two task in my program ,I need to compute time for each task , I read about stopwatch and TimeSpan but I do not know which one must I use and how do that ,I want to measure time for each task in millisecond

for (int num = 2; num < 243; num++)
   {
     do something\\ compute time for first task
     do something\\ compute time for second task

    }
Was it helpful?

Solution

You can easily use Stopwatch for this

 Stopwatch watch = new Stopwatch();

  watch.Start();

  // do something
  watch.Stop();

  Console.WriteLine(watch.ElapsedMilliseconds);
  watch.Reset();

  watch.Start();
  // do something different
  ...

OTHER TIPS

var start = DateTime.Now;
//code to measure
var timeTaken = (DateTime.Now - start).TotalMilliseconds;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top