I need to calculate the time taken to execute each thread.
So I used this code to do it.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,ts.Milliseconds);
Console.WriteLine(elapsedTime, "RunTime");
Now my each thread execute a method work(), this is how it goes:
void work(){
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
//codes,,,
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,ts.Milliseconds);
Console.WriteLine(elapsedTime, "RunTime");
}
So now my question is do I need to create an array of 1000 stopwatch objects or not because say 1000 work() methods are running concurrently.
Your code will work fine as it is, assuming each call is done on a different thread, as you are creating a new
StopWatchobject per thread.That is, each
StopWatchis not a shared resource, so the different threads should not be interfering with other threadStopWatchobjects.