I’ve read all sorts of thing on the internet about how innacurate currentTimeMillis() is on Windows, so I thought I would do on experiment on Windows 7.
I wrote a class that sleeps for less than the putative accuracy of Windows and timed the sleep in nanos and millis. Then I put the ratio of the results (expressed as a percentage) in a table.
`
public class TryItOut {
public static void main(String[] args) throws InterruptedException {
TreeMap<Integer, Integer> multiCounter = new TreeMap<Integer, Integer>();
for (int i = 0; i < 500; i++) {
long nowNano = System.nanoTime();
long nowMilli = System.currentTimeMillis();
Thread.sleep(10L);
long nano = System.nanoTime() - nowNano;
long milli = System.currentTimeMillis() - nowMilli;
int diff = (int) (nano / milli / 10000);
Integer count = multiCounter.get(diff) == null ? 1 : multiCounter.get(diff) + 1;
multiCounter.put(diff, count);
}
for (Map.Entry<Integer, Integer> sortedEntry : multiCounter.entrySet()) {
System.out.println(sortedEntry.getKey() + ":" + sortedEntry.getValue());
}
}
}
`
Now, before someone points out that Thread.sleep() is itself inaccurate, this is discounted by my loop as I look at the proportional (not the absolute) difference between nanos and millis.
Just to be clear, I am not measuring the accuracy of the timers but only the degree to which the timers agree with each other.
These are the results for a 10ms sleep:
91:2 94:1 96:2 97:2 98:6 99:394 100:87 101:3 103:2 106:1
I would say currentTimeMillis() aint that bad!
The spread of course gets wider as you go for smaller sleeps. This is for 5ms sleeps:
82:1 84:1 85:1 88:1 89:1 90:1 91:1 94:2 95:3 96:2 97:10 98:39 99:352 100:59 101:16 102:5 106:1 109:2 110:1 116:1
…but for 60FPS (17ms) the results get quite tight:
94:2 95:5 96:1 97:3 98:19 99:360 100:84 101:17 102:2 103:5 104:2
My benchmark app uses System.currentTimeMillis() because it measures large timescales of at least a second. The difference between nanos and millis for one second intervals is:
99:149 100:351
EDIT: At the risk of belaboring the point, I ran the 1sec test again, with the ratio measured in thousandths:
998 : 5 999 : 113 1000 : 382