1

I want to time total runtime of a progam I am working on.

Currently the code looks similar to this (sw is a member of the program class):

 void Main(string[] args)
 {
     sw.Start();
     //Code here starts a thread
     //Code here joins the thread
     //Code operates on results of threaded operation
     sw.Stop();
     Console.WrtieLine("Operation took {0}", sw.Elapsed.ToString());
 }

I assume this issue is caused by the thread taking control of the program execution but that's the operation that takes the most time and I'm supposed to avoid changing that code as other projects depend on it as well.

For reference, simple observation shows that the code takes nearly half an hour to run, but the elapsed time according to the stopwatch is only 23 seconds or so. Exact output Operation took 00:00:23.1064841

mgbowe1
  • 51
  • 8

1 Answers1

1

In this case, the best solution is to use Environment.TickCount as it measures milliseconds since the system booted and is not processor-core dependent like Stopwatch.

int StartTime = Environment.TickCount;
//Your Code to measure here
int EndTime = Environment.TickCount;
int runningTime = EndTime - StartTIme; //running time in milliseconds
// code to do whatever you want with runningTime
mgbowe1
  • 51
  • 8