
// StopWatch.java
// Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th

/* Stopky realizované pomocí počítadla s vysokým rozlišením sun.misc.Perf. */

import sun.misc.Perf;    // jen na J2SE 1.4.2


public class StopWatch
{
  private Perf hiResTimer; 
  private long freq; 
  private long startTime;

  public StopWatch()
  {
    hiResTimer = Perf.getPerf(); 
    freq = hiResTimer.highResFrequency(); 
  } 


  public void start() 
  {  startTime = hiResTimer.highResCounter(); }


  public long stop() 
  //  vrátí uplynulý čas v nanosekundách
  {  return (hiResTimer.highResCounter() - startTime)*1000000000L/freq;  }


  public long getResolution()
  // vrátí rozlišení počítadla v nanosekundách
  {
    long diff, count1, count2;

    count1 = hiResTimer.highResCounter();
    count2 = hiResTimer.highResCounter();
    while(count1 == count2)
      count2 = hiResTimer.highResCounter();
    diff = (count2 - count1);

    count1 = hiResTimer.highResCounter();
    count2 = hiResTimer.highResCounter();
    while(count1 == count2)
      count2 = hiResTimer.highResCounter();
    diff += (count2 - count1);

    count1 = hiResTimer.highResCounter();
    count2 = hiResTimer.highResCounter();
    while(count1 == count2)
      count2 = hiResTimer.highResCounter();
    diff += (count2 - count1);

    count1 = hiResTimer.highResCounter();
    count2 = hiResTimer.highResCounter();
    while(count1 == count2)
      count2 = hiResTimer.highResCounter();
    diff += (count2 - count1);

   return (diff*1000000000L)/(4*freq);
  } // konec metody: getResolution()

} // konec třídy: StopWatch