Hi
first message on this board glad i can post something usefull:
Here’s my Timing class (note the cast):
//------------------------------------------------------
import sun.misc.Perf;
public class PerfTimer {
Perf hiResTimer;
long freq;
public PerfTimer() {
hiResTimer = Perf.getPerf();
freq = hiResTimer.highResFrequency();
}
public double getTime() {
return (double)hiResTimer.highResCounter() * 1000 / (double)freq;
}
}
//------------------------------------------------------
This is the Testing class:
//------------------------------------------------------
public class Test {
public static void main(String[] args) {
PerfTimer t = new PerfTimer();
double start, end, delta, sumOfDeltas, averagedelta;
int resTestIterations = 1000;
System.out.println("Test1: measure a system-timed intervall");
start = t.getTime();
long sysstart = System.currentTimeMillis();
long sysend;
do { sysend = System.currentTimeMillis(); }
while ( sysend < sysstart + 1000);
end = t.getTime();
long sysdelta = sysend-sysstart;
delta = end-start;
System.out.println("systimer-delta: "+sysdelta);
System.out.println("perftimer-delta: "+delta);
System.out.println("\nTest2: resolution");
sumOfDeltas = 0;
for (int i=0;i<resTestIterations;i++) {
start = t.getTime();
while (start == t.getTime());
end = t.getTime();
delta = end-start;
sumOfDeltas += delta;
}
averagedelta = sumOfDeltas/resTestIterations;
System.out.println("resolution: "+ delta);
System.out.println("average resolution:"+ averagedelta);
System.out.println("\nTest3: measure a perf-timed intervall with sys-timer");
sysstart = System.currentTimeMillis();
start = t.getTime();
do end = t.getTime();
while (end <start+1000);
sysend = System.currentTimeMillis();
delta = end-start;
sysdelta = sysend-sysstart;
System.out.println("Perf-delta:"+delta);
System.out.println("system-Time-delta:"+sysdelta);
}
}
//------------------------------------------------------
its quite long but i couldn’t believe the results first
this is an example output (using Win98se)
Test1: measure a system-timed intervall
systimer-delta: 1040
perftimer-delta: 1002.6182135134682
Test2: resolution
resolution: 0.011733351212569687
average resolution:0.013178229604925037
Test3: measure a perf-timed intervall with sys-timer
Perf-delta:1000.3126100001673
system-Time-delta:980
so this timer actually seems to have ~12E-6 second resolution (is that 12 nanos in english or 12mycro?)
the differences between perf and sys timing are always inside the 50ms-accuracy of the system-timer.
the average resolution goes up and down a bit betweend differnt tests but usually just around 0.002 millis.
lets hope sun has a heart for gamers and leaves this timer!