new Date(), System.currentTimeMillis() and system clock accuracy



Hi,
Im having a problem getting a higher resolution than 10ms on my system
clock (on XP) using both the getTime() method in Date and the
currentTimeMillis() method in System to measure the time elapsed
between two points in my code. I wrote this small peice to test this
resolution:

long milliseconds;
for(int i = 0; i != 50; i++){
long time1 = System.currentTimeMillis();
long time2;
milliseconds = 0;
while(milliseconds < 1){
time2 = System.currentTimeMillis();
milliseconds = time2 - time1;
}
System.out.println("currentTimeMillis(): " + milliseconds + "ms");
}
for(int i = 0; i != 50; i++){
Date date1 = new Date();
Date date2;
milliseconds = 0;
while(milliseconds < 1){
date2 = new Date();
milliseconds = date2.getTime() - date1.getTime();
}
System.out.println("new Date(): " + milliseconds + "ms");
}

Each method assumedly gets its time from the same place since they
return very much the same values (lots of 10ms and the occasional
20ms). Is there some way to update the system clock before checking
it? I really need a granularity of 1ms for my program to work.

-Numeron
.



Relevant Pages

  • Re: new Date(), System.currentTimeMillis() and system clock accuracy
    ... Im having a problem getting a higher resolution than 10ms on my system ... long milliseconds; ...   long time1 = System.currentTimeMillis; ... Is there some way to update the system clock before checking ...
    (comp.lang.java.help)
  • Re: simple time server
    ... But I would still need an SNTP server program unlike what's available ... that and it should keep your clock within a hundred milliseconds of the ... server unless you have a machine with a really bad local clock. ... For my computers on the Internet, I have been doing something like ...
    (comp.protocols.time.ntp)
  • Re: Post processing of NTP data...
    ... When queried as to the time using O/S services, these systems respond with the current value of the clock register. ... milliseconds and the typical error is 5 milliseconds. ... Some very new hardware designs allow ntpd to interpolate between "ticks" and yield a much more precise time, if and only if, you use NTP supplied functions to get the time. ...
    (comp.protocols.time.ntp)
  • Re: Accurate Timing in ruby
    ... I'm attempting to make an action-driven game with ruby ... I fear what you see is not Ruby's fault, but your hardware/OS clock. ... You don't round to milliseconds before you compute the fps, ... on the action-- is quite possible on today's hardware; ...
    (comp.lang.ruby)
  • Re: Get system time in Milliseconds
    ... you need to watch the clock in a tight loop. ... that you get the system time and then call GetTickCount and use modulo ... GetTickCountreturn milliseconds since the PC restart. ...
    (microsoft.public.windowsce.app.development)