import sun.misc.Perf;

public final class Timer {
	Perf _timer;
	long _ticksPerSecond;						// nano sec
	
	long _startTime	= 0;

    public Timer() {
		restart();
    }

	// Return elapsed time from the previous polling
	public double elapsedTimeInMs(){
		long currTick 		= _timer.highResCounter();
		double currTimeInMs	= ((currTick - _startTime) * 1000) / (double)_ticksPerSecond;
		
		_startTime = currTick;					// set start time 
		
		return currTimeInMs;
	}

	// Return TOTAL elapsed time from the begining of the timer
	public double totalElapsedTimeInMs(){
		long currTick 		= _timer.highResCounter();
		double currTimeInMs	= ((currTick - _startTime) * 1000) / (double)_ticksPerSecond;
		
		return currTimeInMs;
	}
	
	public void reset(){
		_startTime	= _timer.highResCounter();
	}
	
	public void restart(){
		_timer = sun.misc.Perf.getPerf();			// "perfect" timer
		 											// This is an undocumented object.
													// Need to be causious to use
												
		_ticksPerSecond = _timer.highResFrequency();
	}
	
}
