I’m totally confused between these 4. What is the difference between ElapsedMilliseconds (long), ElapsedTicks (long), Elapsed.TotalMilliseconds (double) and Elapsed.Milliseconds (int)?
I have a function
{
Stopwatch sw = new Stopwatch();
sw.Start();
MyTimeConsumingAction();
sw.Stop();
sw.//what?
}
How do I get the correct time consumed by my long running process from elapsed property of Stopwatch object in milliseconds?
Edit: I tried msdn documentation but it isn’t anything detailed there..
e.g. a stopwatch stopped at 1.23456 seconds would return 1234.56 in this property. See TimeSpan.TotalMilliseconds on MSDN
e.g. a stopwatch at 1.234 seconds would return 234 in this property. See TimeSpan.Milliseconds
In the context of the original question, pertaining to the Stopwatch class,
ElapsedTicksis the number of ticks elapsed. Ticks occur at the rate ofStopwatch.Frequency, so, to compute seconds elapsed, calculate:numSeconds = stopwatch.ElapsedTicks / Stopwatch.Frequency.The old answer defined ticks as the number of 100 nanosecond periods, which is correct in the context of the DateTime class, but not correct in the context of the Stopwatch class. See Stopwatch.ElapsedTicks on MSDN.
Elapsed.TotalMillisecondsis adoublethat can return execution times to the partial millisecond whileElapsedMillisecondsisInt64. e.g. a stopwatch at 0.0007 milliseconds would return 0, or 1234.56 milliseconds would return 1234 in this property. So for precision always useElapsed.TotalMilliseconds.See Stopwatch.ElapsedMilliseconds on MSDN for clarification.