Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with compiling Microseconds for JNI



You don't need native code.

Apple has implemented an excellent nanosecond timer for Java.

- Craig

/**
	Test Sun's performance measurement clock
*/

import sun.misc.Perf;
import java.text.DecimalFormat;

public class TestTimer
{
	/////////////////////////
	// Class instance vars //
	/////////////////////////
	final HiResTimer timer;

	/////////////////
	// Constructor //
	/////////////////
	public TestTimer()
	{
		long before, after, cputime;	// CPU timing variables

		final DecimalFormat sci = new DecimalFormat("##0.###E0");

		timer = new HiResTimer();

System.out.println("Counter frequency : " + sci.format(timer.getFrequency()) + " Hz (ticks/sec)");
System.out.println("Time per clock tick : " + timer.getTickTime() + " nanoseconds");
System.out.println("Counter resolution : " + timer.getCounterResolution() + " clock ticks");
System.out.println("Avg. counter resolution: " + timer.getAvgCounterResolution() + " clock ticks");
System.out.println("Time resolution : " + timer.getResolution() + " nanoseconds");


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()");
}


	/////////////////////
	// Sleep 3 seconds //
	/////////////////////
	private final void snooze()
	{
		try
		{
			Thread.sleep(3000);
		}
		catch ( Exception x )
		{
			x.printStackTrace();
		}
	}

	//////////
	// 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;

		/////////////////
		// Constructor //
		/////////////////
		public HiResTimer()
		{
			hiResTimer = Perf.getPerf();
			freq = hiResTimer.highResFrequency();
		}

 		//////////////////////////////////
 		// 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

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.