在一个类中拥有两个 NSTimer 会导致它们不太准确吗?
我有一个 NSTimer,它是我的主游戏循环,它每秒调用 60 次。我有另一个计时器,它是一个倒计时,每秒被调用 0.001 次。定时器不准确正常吗?或者我应该每秒调用倒数计时器更少的次数?
I have one NSTimer which is my main game loop, it calls 60 times a second. I have another timer which is a countdown, which gets called 0.001 times a second. Is it normal that timer is not accurate? or should i call the countdown timer fewer times per second?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSTimers 不准确。您指定的时间间隔只是一个目标。 NSTimer 将尝试达到该目标,但是在该线程上运行的东西越多,循环时间就越慢。您的 0.001 计时器可能运行得太快而无法使用,并且会出现准确性问题。如果您需要实时准确性,则必须使用 NSDate 跟踪计时器实际触发的时间并进行相应补偿。
NSTimers are not accurate. The time interval you specify is simply a goal. The NSTimer will try to hit that goal but the more stuff you have running on that thread the slower the cycle time will be. Your 0.001 timer is probably going way too fast to be useful and will suffer from accuracy problems. If you need real time accuracy you will have to track what time the timer is actually firing with an NSDate and compensate accordingly.
如果我正确地阅读你的问题,第二个计时器(我假设每 0.001 秒触发一次)将不会准确。
NSTimer 的分辨率约为50-100 毫秒(0.05s-0.1s),如果加载运行循环,这可能会受到显着影响。
If I'm reading your question correctly the second timer (which I assume is fired every 0.001) seconds is not going to be accurate.
NSTimer has a resolution of about 50-100 milliseconds (0.05s-0.1s) and this can be significantly impacted if your run loop is loaded.