开发者

Android Simple Timing Question

开发者 https://www.devze.com 2023-02-20 01:34 出处:网络
I need to implement a millisecond-accurate timer for a small game. Which method would be the most appropriate for this? My current approach is to use System.currentTimeMillis(). It s开发者_C百科eems t

I need to implement a millisecond-accurate timer for a small game. Which method would be the most appropriate for this? My current approach is to use System.currentTimeMillis(). It s开发者_C百科eems to work well enough, but maybe there's some pitfall I'm not aware of. What would be the standard approach to timing?


Your timer will only be as good as your system implements it. It simply won't get better than that.

Being a ex-professional game developer (and still do side projects) I would recommend against a millisecond-accuracy requirement. You probably don't need it.

Determine your clock accuracy with this program:

/*
C:\junk>javac TimerTest.java

C:\junk>java TimerTest
Your delay is : 16 millis

C:\junk>
*/
class TimerTest {

    public static void main(String[] args) {
        long then = System.currentTimeMillis();
        long now = then;
            // this loop just spins until the time changes.
            // when it changes, we will see how accurate it is
        while((now = System.currentTimeMillis()) == then); // busy wait
        System.out.println("Your delay is : " + (now-then) + " millis");

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号