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
jonbbbb - 04 Jul 2008 02:17 GMT
On Jul 4, 3:05 am, Numeron <irunsofastineedafinonmyh...@hotmail.com>
wrote:
> Hi,
> Im having a problem getting a higher resolution than 10ms on my system
[quoted text clipped - 32 lines]
>
> -Numeron
Looks like it only handles 10ms on Windows Xp:
http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=eb9d3ee134a0ad25c67faba39
ef5?bug_id=4313758
"For example, many operating systems measure time in units of tens of
milliseconds. "
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#currentTimeMillis()
Jon.
Numeron - 04 Jul 2008 02:29 GMT
> [snip]
> Looks like it only handles 10ms on Windows Xp:
> [snip]
Thanks for the speedy reply that was only posted like 10 minutes
ago :)
It seems then that on this machine the problem is unsolvable without
trying to simulate ~1ms in a busy method. Something which is not only
difficult, but really bad programming practice (busy methods are the
devil on many computers). Not cool. I see that the Thread class has a
sleep(millis) method. Is there any way to query how long a thread has
been sleeping (I dont see anything obvious in the api), and if so is
this any more accurate than the system clock?
-Numeron
Daniel Pitts - 06 Jul 2008 01:25 GMT
>> [snip]
>> Looks like it only handles 10ms on Windows Xp:
[quoted text clipped - 12 lines]
>
> -Numeron
Thread.sleep() is also constrained by the system clock resolution, as
well as by arbitrary scheduling algorithms. The truth is that
Thread.sleep() gives up control for at *least* n milliseconds.
If you really need more precise timing, you may have to find/write some
JNI code that creates a low-level timer with more resolution than the OS
defaults. This code is likely to be extremely system specific.
I don't think there is any way to do what you want to do in pure Java.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Roedy Green - 04 Jul 2008 03:04 GMT
On Thu, 3 Jul 2008 18:05:05 -0700 (PDT), Numeron
<irunsofastineedafinonmyhead@hotmail.com> wrote, quoted or indirectly
quoted someone who said :
>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:
see http://mindprod.com/jgloss/time.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
John B. Matthews - 04 Jul 2008 19:34 GMT
> On Thu, 3 Jul 2008 18:05:05 -0700 (PDT), Numeron
> <irunsofastineedafinonmyhead@hotmail.com> wrote, quoted or indirectly
[quoted text clipped - 7 lines]
>
> see http://mindprod.com/jgloss/time.html
Expanding on the example, I wrote the program below to collect
OS-specific timer resolution data, as described in Roedy's article:
<sscce>
public class MilliRes {
public static void main(String[] args) {
final int COUNT = 1000;
long minMillis = Long.MAX_VALUE;
long maxMillis = Long.MIN_VALUE;
int sum = 0;
int index = 0;
while (index < COUNT) {
final long start = System.currentTimeMillis();
long delta = 0;
while(delta < 1) {
final long now = System.currentTimeMillis();
delta = now - start;
}
minMillis = delta < minMillis ? delta : minMillis;
maxMillis = delta > maxMillis ? delta : maxMillis;
sum += delta;
index++;
}
System.out.println(whichOS());
System.out.println("min: " + minMillis + "ms");
System.out.println("max: " + maxMillis + "ms");
System.out.println("avg: " + (float) sum / COUNT + "ms");
}
private static String whichOS() {
java.util.Properties p = System.getProperties();
return p.getProperty("os.name")
+ ", " + p.getProperty("os.version")
+ ", " + p.getProperty("os.arch")
+ ", " + p.getProperty("java.version");
}
}
</sscce>

Signature
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
Patricia Shanahan - 04 Jul 2008 03:24 GMT
> 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:
...
If you are using 1.5 or later, try System.nanoTime().
Patricia
Numeron - 04 Jul 2008 04:19 GMT
> If you are using 1.5 or later, try System.nanoTime().
This works with around 1600 nanosecond accuracy (.0016ms) on my
machine, thanks!
Roedy Green wrote:
> see http://mindprod.com/jgloss/time.html
Very helpful, thanks!
-Numeron