Possible Duplicate:
Environment.TickCount vs DateTime.Now
In my application I require fast timing operations but accuracy is not that important. I checked the runtime speed of the three timing operations for which I am familiar with and came up with these results:
for (int i = 0; i < 1000000; i++)
{
// var time = (DateTime.Now - dt).TotalMilliseconds; // 1131 ms
// var time = (Environment.TickCount - dt); // 7 ms
// var time = stopwatch.ElapsedMilliseconds; // 131 ms
}
The times were checked using a Stopwatch
. So, I want to use Environment.TickCount
(I think!) but its problem is that after 24.9 days if the user is very unlucky the operation of 2,147,483,647 - -2,147,483,648
will occur and result in an overflow error and crash the program.
So, my question is two-fold. Firstly, is Environment.TickCount
the best tool for the job given my specified goals, and secondly, whether anyone else has written a wrapper class for it and if so, how did you take in to account the rare overflow chance?