System.out.println("Sleeping for 3 seconds . . .");
before = System.currentTimeMillis(); // timer.milliTime();
snooze();
after = System.currentTimeMillis(); // timer.milliTime();
cputime = after - before;
System.out.println("Sleep time of 3 seconds measured as: " + cputime
+ " milliseconds using System.currentTimeMillis()");
System.out.println("Sleeping again for 3 seconds . . .");
before = timer.nanoTime(); // System.nanoTime();
snooze();
after = timer.nanoTime(); // System.nanoTime();
cputime = after - before;
System.out.println("Sleep time of 3 seconds measured as: " + cputime
+ " nanoseconds using timer.nanoTime()");
System.out.println("Sleeping yet again for 3 seconds . . .");
before = timer.getTicks();
snooze();
after = timer.getTicks();
cputime = timer.nsFromTicks(after - before);
System.out.println("Sleep time of 3 seconds measured as: " + cputime
+ " nanoseconds from ticks using timer.getTicks()");
}
//////////
// main //
//////////
public static void main(final String[] args)
{
new TestTimer();
}
//////////////////////////////////////////////////
// Inner class for high-rez timing measurements //
// -- substitute for Tiger's System.nanoTime() //
//////////////////////////////////////////////////
public class HiResTimer
{
/////////////////////////
// Class instance vars //
/////////////////////////
private final int LOOPS = 1000000;
private final boolean BEST = true;
private final Perf hiResTimer;
private final long freq;
//////////////////////////////////
// Current time in milliseconds //
//////////////////////////////////
public long milliTime()
{
return (hiResTimer.highResCounter() * 1000L / freq);
// return (hiResTimer.highResCounter() / ((freq + 500L) / 1000L));
}
/////////////////////////////////
// Current time in nanoseconds //
/////////////////////////////////
public long nanoTime()
{
return (hiResTimer.highResCounter() * 1000000000L / freq);
}
/////////////////////////
// Current clock ticks //
/////////////////////////
public long getTicks()
{
return hiResTimer.highResCounter();
}
//////////////////////////////////////
// Number of clock ticks per second //
//////////////////////////////////////
public long getFrequency()
{
return freq;
}
//////////////////////////////////////////
// Number of nanoseconds per clock tick //
//////////////////////////////////////////
public long getTickTime()
{
return (1000000000L/freq);
}
////////////////////////////////////////
// Convert clock ticks to nanoseconds //
////////////////////////////////////////
public long nsFromTicks(long ticks)
{
return (ticks * 1000000000L / freq);
}
//////////////////////////////////////////////////
// Error expected in measured elapsed time (ns) //
//////////////////////////////////////////////////
public long getResolution()
{
long before = 0L;
long after = 0L;
long smallest = Long.MAX_VALUE;
if (BEST)
{
// Compute using clock ticks
for (int i=0; i<LOOPS; ++i)
{
before = hiResTimer.highResCounter();
after = hiResTimer.highResCounter();
smallest = Math.min(smallest, after - before);
}
return (smallest * 1000000000L / freq); // ticks --> ns
}
else
{
// Compute using nanoseconds
for (int i=0; i<LOOPS; ++i)
{
before = nanoTime(); // System.nanoTime();
after = nanoTime(); // System.nanoTime();
smallest = Math.min(smallest, after - before);
}
return (smallest);
}
}
///////////////////////////////////////////////////////////
// Error expected in measured elapsed time (clock ticks) //
///////////////////////////////////////////////////////////
public long getCounterResolution()
{
long before = 0L;
long after = 0L;
long smallest = Long.MAX_VALUE;
for (int i=0; i<LOOPS; ++i)
{
before = hiResTimer.highResCounter();
after = hiResTimer.highResCounter();
smallest = Math.min(smallest, after - before);
}
return smallest;
}
///////////////////////////////////////////////////////////////
// Avg error expected in measured elapsed time (clock ticks) //
///////////////////////////////////////////////////////////////
public long getAvgCounterResolution()
{
long before = 0L;
long after = 0L;
long dt = 0L;
// Warm up JIT/HotSpot compiler
// for (int i=0; i<LOOPS; ++i)
// before = hiResTimer.highResCounter();
for (int i=0; i<LOOPS; ++i)
{
// before = after = hiResTimer.highResCounter();
// while (before == after)
// after = hiResTimer.highResCounter();
before = hiResTimer.highResCounter();
after = hiResTimer.highResCounter();
dt += (after - before);
}
return Math.round((double)dt/LOOPS);
}
} // End HiResTimer inner class
} // End TestTimer class
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden