Re: new Date(), System.currentTimeMillis() and system clock accuracy
In article <t91r649caogfo0sd1teqp914ctlp7idcgp@4ax.com>,
Roedy Green <see_website@mindprod.com.invalid> wrote:
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
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>
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews