// package timer; import java.util.*; /** * timer class * realize stop-watch function This timer seems to do millisecond accurate timings * * @version 0.1 * @author S.Honda */ public class timer { private Calendar cal; private Calendar origin; private Calendar stop; public timer() { String[] ids = TimeZone.getAvailableIDs(9 * 60 * 60 * 1000); if (ids.length == 0) { System.err.println( "can not get available timezone" ); return; } SimpleTimeZone pdt = new SimpleTimeZone(9 * 60 * 60 * 1000, ids[0]); cal = new GregorianCalendar(pdt); } public void reset() { origin = cal.getInstance(); } public void stop() { stop = cal.getInstance(); } public void display( Calendar c ) { System.out.println( "" + c.get(Calendar.HOUR) +":"+ c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND) + ":" + c.get(Calendar.MILLISECOND) ); } long toMilliSecond( Calendar c ) { long ret = (long)c.get(Calendar.HOUR) * 60L * 60L *1000L + (long)c.get(Calendar.MINUTE) *60L *1000L + (long)c.get(Calendar.SECOND) * 1000L + (long)c.get(Calendar.MILLISECOND); return ret; } public long getDiff() { long o = toMilliSecond( origin ); long s = toMilliSecond( stop ); return ( s - o ); } /* public static void main ( String[] args ) { timer t = new timer(); t.reset(); */ /* make it do about 3.6 seconds worth of work on my 200 mhz * pentium. Or about 23.2 seconds worth on my sun sparc lx. */ /* int j = 1; for (int i = 0; i < 100000; i++) for(int k = 0; k < 100; k++) j = j * j; t.stop(); System.out.println( "the time difference is " + t.getDiff() ); } */ }