granularitytest.java

来自「Developing Games in Java 源代码」· Java 代码 · 共 31 行

JAVA
31
字号
/**
    Measures the granularity of System.currentTimeMillis().
*/
public class GranularityTest {

    public static final int NUM_SAMPLES = 100;

    public static void main(String[] args) {
        long sum = 0;
        int count = 0;

        long lastTime = System.currentTimeMillis();
        while (count < NUM_SAMPLES) {
            long currTime = System.currentTimeMillis();

            // if the time changed, record the difference
            if (currTime > lastTime) {
                long granularity = currTime - lastTime;
                // keep a running sum of the granularity
                sum+=granularity;
                count++;
                lastTime = currTime;
            }
        }

        // display results
        System.out.println("Average granularity of " +
            "System.currentTimeMillis(): " + ((float)sum / count));
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?