- Home /
How to measure time independently on frame rate?
I am recieving data every server tick (25 ticks per second) and I need to count ticks per second on client. I am using Time.time for measuring those ticks (I tested even Time.fixedTime) and when I have Vsync turned on I see 26t/s, when half vsync I see 27t/s.
I know that Time.time ansd Time.fixedTime is counted by number of screen refreshes, but I need to count ticks independtly on screen refresh rate? Is there something like microtime() in PHP? I need time what is independent on screen to properly calculate ticks per second.
Answer by Xarbrough · Dec 21, 2017 at 01:17 AM
Time.time and Time.fixedTime are not "counted by screen refreshes", they are just not very accurate overall. You can also use link text to not scale it by timeScale etc. However, again, not very accurate. Most likely this function calls something similar to System.DateTime.Now from the .NET framework. The later you can also try if you just want a time that is exact for around one second.
For precise measurements, the Stopwatch class, as mentioned by Buckslice, is better, but then again, it's never going precise enough to sync specific timing to one another, but if you are only displaying some "x per second" data it should work fine.
Stopwatch and System.DateTime not working too... When I have vsync, I am getting 27t/s and when turned off its ok with 25t/s Still depends on framerate then...
Are you starting and stopping your time measurement in Update? I see the problem, you can't call Time.time from a different thread, but the Stopwatch and DateTime classes should be thread-safe. In that case I would try running my own timer-thread or even use a threaded timer from the .NET framework. If you can post a simple code example, I could make one, too. ;)
Your answer
