문제

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

    }
도움이 되었습니까?

해결책

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
  ...

다른 팁

var start = DateTime.Now;
//code to measure
var timeTaken = (DateTime.Now - start).TotalMilliseconds;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top