I am trying to make a small game like app that has some interaction, but need an accurate enough time to evaluate the game.
I saw some java versions using this method called System.nanoTime
.
Is there a .NET version for it?
More importantly I am looking for a some sort of timer that I can use to evaluate the world. Should I be using an actual timer control and subscribe to it?
Also the game evaluates on a different thread than the UI thread. Just in case this makes a difference.
DateTime.Now
does not have an actual resolution in the nanosecond range. A Tick is a 100 nanosecond "chunk" of time but DateTime.Now
does not resolve each Tick. I believe the lowest resolution for DateTime.Now
is 10 milliseconds or so. If you call DateTime.Now
twice very quickly, you'll get the same answer. You'll only see it tick up after about 10 milliseconds or so, depending on where in the cycle you are calling it.
The Stopwatch class has a finer resolution that DateTime.Now
. I believe it will resolve down to the millisecond, which should hopefully give you enough resolution to determine if your game evaluation code is performing well enough. There is a great example that contrasts Stopwatch vs. DateTime at the link provided.
Also, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With