The clock accuracy on Windows seems to have changed lately. I remember it being very accurate since Windows 7 (1 ms resolution), and now the time hops in steps of 15 to 16 ms. I noticed due to some (albeit poorly written) unit tests failing (tests checked that some time elapsed between writing and reading records).
This affects following implementations:
System.currentTimeMillis()
LocalTime.now()
LocalDateTime.now()
ZonedDateTime.now()
I'm well aware that elapsed time is to be measured using System.nanoTime()
deltas, but I'm wondering nevertheless if I missed something that changed back the clock resolution to 15-16ms.
Environment:
- Windows 10 version 1709
- JRE/JDK 1.8.0_171-b11 (64-bit)
Did anyone notice this as well? What could be the reason for this resolution change?
EDIT:
Test code to check this behaviour (see output: time changes and number of samples before change)
@Test
public void testCurrentTimeMillis() {
test(() -> System.currentTimeMillis());
}
@Test
public void testNanoTime() {
test(() -> System.nanoTime());
}
@Test
public void testLocalTime() {
test(() -> LocalTime.now());
}
@Test
public void testLocalDateTime() {
test(() -> LocalDateTime.now());
}
@Test
public void testZonedDateTime() {
test(() -> ZonedDateTime.now());
}
private <T> void test(Supplier<T> timeSupplier) {
int samples = 20;
String lastTimeString = null;
int count = 0;
while (samples > 0) {
count++;
String timeString = timeSupplier.get().toString();
if (!timeString.equals(lastTimeString)) {
System.out.println(timeString + " (" + count + ")");
lastTimeString = timeString;
count = 0;
samples--;
}
}
}