// TimerAccuracy.java
//**********************************************************************************************
//
//
//                              (C) Copyright 2000 by Dipl. Phys. Joerg Plewe, HARDCODE Development
//                                      All rights reserved. Copying, modification,
//                                      distribution or publication without the prior written 
//                                      consent of the author is prohibited.
//
//
//      Filename:               TimerAccuracy.java
//      Author:                 Herkules
//  Created:            20000119
//  Version:            $Version$
//
//      Module:                 $Module$
//  Dependecies:        $Dependencies$
//
//  History:
//  $Date$              $Who$           $What$
//
/*      Purpose:

$Describe purpose of module$
Measure the resolution of the System timer

*/
//**********************************************************************************************


package HARDCODE.Ext.Util;


/**
 * Measure the accuracy of the timer
 */


//
// This classes 'measure' method has to be run at highest thread priority!
//
public class TimerAccuracy
{
	
        final static public long measure( long _timeToMeasure )
        {
                // Max./min. ticker difference
                maxRes = 0;
                minRes = 999999;
        	
                // Get current ms ticker
                long ms = System.currentTimeMillis();

                // Calculate time to stop
                long EndMs = ms + _timeToMeasure;
        	
                while( ms < EndMs )
                {	
                        long oldms = ms;

                        ms = System.currentTimeMillis();

                        long diff = ms - oldms;
                	
                        if( diff > maxRes )
                                maxRes = diff;
                	
                        if( diff > 0 && diff < minRes )
                                minRes = diff;
                }

                System.out.println( "Worst timer resolution: " + maxRes ); 
                System.out.println( "Best timer resolution: " + minRes ); 

                return minRes;
        }

	
        //*************************************************************************
        // Variables
        //*************************************************************************
        static public long minRes = -1;
        static public long maxRes = -1;

} // TimerAccuracy
