|
From: Brent B. <bre...@us...> - 2006-11-19 22:24:13
|
Update of /cvsroot/jigs//jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27624/src/edu/whitman/halfway/util Modified Files: ExecutionTimer.java TimingTree.java StringUtil.java Timing.java Log Message: Modified ExecutionTimer to reflect Brent's StopWatch class. This involved adding new methods and eliminating / replacing a confusing method. ET's are now in terms of ns, but return ms for default methods. Timings was changed to reflect changes in ET. Also, it was genericized to make loops more efficient. Index: ExecutionTimer.java =================================================================== RCS file: /cvsroot/jigs//jigs/src/edu/whitman/halfway/util/ExecutionTimer.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ExecutionTimer.java 10 Aug 2006 22:53:35 -0000 1.4 --- ExecutionTimer.java 19 Nov 2006 22:24:09 -0000 1.5 *************** *** 4,80 **** public final class ExecutionTimer{ ! protected static Logger log = Logger.getLogger(ExecutionTimer.class); ! ! boolean isRunning = false; ! long elapsedTime = 0; ! long lastStartTime = 0; ! ! protected String name; ! ! public ExecutionTimer(){ ! this("Unnamed"); ! } ! ! public ExecutionTimer(String name){ ! this.name = name; ! } ! ! public String getName() { ! return name; ! } ! public void setName(String name) { this.name = name; } ! ! public boolean isRunning(){ ! return isRunning; ! } ! ! public final void reset(){ ! isRunning = false; ! elapsedTime = 0; ! lastStartTime = 0; ! } ! ! public final void start(){ ! if(isRunning){ ! log.warn(String.format( ! "Timer %s is already running, ignoring call to start.", ! getName())); ! //throw new IllegalArgumentException("Timer is already running."); ! } ! isRunning = true; ! lastStartTime = System.currentTimeMillis(); ! } ! public long stop(){ ! return stop(System.currentTimeMillis()); ! } ! ! public void incTime(long ms){ ! elapsedTime += ms; ! } ! ! public long stop(long stopTime){ ! if(!isRunning){ ! log.warn("Timer " + getName() + " isn't running, so can't stop it."); ! //throw new IllegalArgumentException("Can't stop a timer that isn't running"); ! } ! long delta = stopTime - lastStartTime; ! elapsedTime += delta; ! isRunning = false; ! return delta; ! } ! public final long getElapsedTime(){ ! if(!isRunning) return elapsedTime; ! return elapsedTime + (System.currentTimeMillis() - lastStartTime); ! } ! ! public String toString(){ ! return StringUtil.formatMillis(getElapsedTime()); ! } ! } --- 4,153 ---- public final class ExecutionTimer{ ! private static final long nsInMs = 1000000; ! protected static Logger log = Logger.getLogger(ExecutionTimer.class); ! protected final String name; ! ! boolean isRunning = false; ! long elapsedTime = 0L; ! long lastStartTime = 0L; ! ! public ExecutionTimer(){ ! this("Unnamed"); ! } ! ! public ExecutionTimer(String name){ ! this.name = name; ! } ! ! public String getName() { ! return name; ! } ! ! /* public void setName(String name) { this.name = name; } ! */ ! ! public boolean isRunning(){ ! return isRunning; ! } ! ! ! public final void reset(){ ! // isRunning = false; ! elapsedTime = 0L; ! lastStartTime = 0L; ! } ! ! ! public final void start(){ ! if(isRunning()){ ! log.warn("Timer "+getName()+" is already running, ignoring call to start."); ! //throw new IllegalArgumentException("Timer is already running."); ! } ! isRunning = true; ! lastStartTime = System.nanoTime(); ! } ! ! public final void start(boolean reset) { ! if (reset) { ! reset(); ! } ! start(); ! } ! /** ! * Stops the Timer ! */ ! public ExecutionTimer stop(){ ! return stop(System.nanoTime()); ! } ! ! /** ! * Adds additional time (in ms) to the elapsed time of the watch. This method ! * does not depend on whether or not the watch is running. ! */ ! public void addTime(long ms){ ! elapsedTime += ms*1000000; ! } ! /** ! * Adds additional time (in ns) to the elapsed time of the watch. This method ! * does not depend on whether or not the watch is running. ! */ ! public void addTimeNano(long ns) { ! elapsedTime += ns; ! } ! ! /** ! * Stops the watch at the specified time. This is useful, if some processing must be done ! * between the time when the timed event ends and the time at which the stop method can be ! * called. StopTime is in nanoSeconds! ! */ ! public ExecutionTimer stop(long stopTime){ ! if(!isRunning()){ ! log.warn("Timer " + getName() + " isn't running, so can't stop it."); ! //throw new IllegalArgumentException("Can't stop a timer that isn't running"); ! } ! long delta = stopTime - lastStartTime; ! elapsedTime += delta; ! lastStartTime = delta; ! isRunning = false; ! return this; ! } ! /** ! * Return the time elapsed between the last start and stop of the timer if the ! * timer is stopped, or the time elapsed between the last start and now if the ! * timer is running in ms. This method explicitly precludes all execution time ! * before the start was last called. ! */ ! public final long getLastEventTime() { ! return getLastEventTimeNano()/nsInMs; ! } ! ! public final long getLastEventTimeNano() { ! if (!isRunning) { ! return lastStartTime; ! } ! return System.nanoTime() - lastStartTime; ! } ! ! /** Return the total elapsed time in ms */ ! public final long getElapsedTime(){ ! return getElapsedTimeNano()/nsInMs; ! } ! public final long getElapsedTimeNano(){ ! if(!isRunning()) { ! return elapsedTime; ! } ! return elapsedTime + (System.nanoTime() - lastStartTime); ! } ! ! public String toString(){ ! return StringUtil.formatMillis(getElapsedTime()); ! } ! ! public static String formatTime(long ms) { ! return formatMicroTime(ms*1000); ! } ! ! public static String formatNanoTime(long ns) { ! return formatMicroTime(ns/1000); ! } ! ! private static String formatMicroTime(long ts) { ! int min = (int)(ts/60000000); ! ts -= min*60000000; ! ! int sec = (int)(ts/1000000); ! ts -= sec*1000000; ! ! int ms = (int)(ts/1000); ! ts -= ms*1000; ! ! return String.format("%4dm %2ds %3d.%03dms", min, sec, ms, ts); ! } ! } \ No newline at end of file Index: TimingTree.java =================================================================== RCS file: /cvsroot/jigs//jigs/src/edu/whitman/halfway/util/TimingTree.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TimingTree.java 11 Sep 2006 15:05:22 -0000 1.2 --- TimingTree.java 19 Nov 2006 22:24:09 -0000 1.3 *************** *** 66,73 **** protected void incTime(long ms){ ! activeNode.timer.incTime(ms); Node parent = activeNode.parent; while(parent != null){ ! parent.timer.incTime(ms); parent = parent.parent; } --- 66,73 ---- protected void incTime(long ms){ ! activeNode.timer.addTime(ms); Node parent = activeNode.parent; while(parent != null){ ! parent.timer.addTime(ms); parent = parent.parent; } *************** *** 204,208 **** } } ! sumNode.timer.incTime(time); for(Object entryObj : childListMap.entrySet()){ --- 204,208 ---- } } ! sumNode.timer.addTime(time); for(Object entryObj : childListMap.entrySet()){ Index: StringUtil.java =================================================================== RCS file: /cvsroot/jigs//jigs/src/edu/whitman/halfway/util/StringUtil.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** StringUtil.java 11 Sep 2006 15:05:22 -0000 1.14 --- StringUtil.java 19 Nov 2006 22:24:09 -0000 1.15 *************** *** 81,86 **** Calendar c = Calendar.getInstance(); ! return c.get(c.MONTH) + "-" + c.get(c.DAY_OF_MONTH) + "-" + c.get(c.YEAR) ! + "_" + c.get(c.HOUR_OF_DAY) + "h-" + c.get(c.MINUTE) + "m"; } --- 81,87 ---- Calendar c = Calendar.getInstance(); ! return c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + ! "-" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR_OF_DAY) + ! "h-" + c.get(Calendar.MINUTE) + "m"; } *************** *** 130,136 **** } ! public static AbstractList tokenize(String string) { StringTokenizer st = new StringTokenizer(string); ! ArrayList tokens = new ArrayList(); while (st.hasMoreTokens()) { tokens.add(st.nextToken()); --- 131,137 ---- } ! public static List<String> tokenize(String string) { StringTokenizer st = new StringTokenizer(string); ! ArrayList<String> tokens = new ArrayList<String>(); while (st.hasMoreTokens()) { tokens.add(st.nextToken()); *************** *** 139,146 **** } /** Tokenizes the string, treating strings quoted with " as a * single entry. Also parses numbers. The resulting List thus * has Strings and Doubles. Returns null if parsing fails.*/ ! public static AbstractList tokenizeWithQuotes(String string) { // StreamTokenizer seems to be pretty badly broken ... it // won't parse strings like "../CMU/Climbing" if number --- 140,157 ---- } + public static List<String> tokenize(String string, String delim) { + StringTokenizer st = new StringTokenizer(string, delim); + ArrayList<String> tokens = new ArrayList<String>(); + while (st.hasMoreTokens()) { + tokens.add(st.nextToken()); + } + return tokens; + } + + /** Tokenizes the string, treating strings quoted with " as a * single entry. Also parses numbers. The resulting List thus * has Strings and Doubles. Returns null if parsing fails.*/ ! public static List<Object> tokenizeWithQuotes(String string) { // StreamTokenizer seems to be pretty badly broken ... it // won't parse strings like "../CMU/Climbing" if number *************** *** 160,164 **** st.slashSlashComments(false); st.slashStarComments(false); ! LinkedList tokens = new LinkedList(); while (st.nextToken() != StreamTokenizer.TT_EOF) { if (st.ttype == StreamTokenizer.TT_WORD || st.ttype == '"') { --- 171,175 ---- st.slashSlashComments(false); st.slashStarComments(false); ! LinkedList<Object> tokens = new LinkedList<Object>(); while (st.nextToken() != StreamTokenizer.TT_EOF) { if (st.ttype == StreamTokenizer.TT_WORD || st.ttype == '"') { Index: Timing.java =================================================================== RCS file: /cvsroot/jigs//jigs/src/edu/whitman/halfway/util/Timing.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Timing.java 29 Aug 2006 19:29:56 -0000 1.2 --- Timing.java 19 Nov 2006 22:24:09 -0000 1.3 *************** *** 29,33 **** private static Timing t = new Timing(); ! protected Map map = new HashMap(); --- 29,33 ---- private static Timing t = new Timing(); ! protected Map<String,ExecutionTimer> map = new HashMap<String,ExecutionTimer>(); *************** *** 63,67 **** //we want to be as precise as possible, so we first read off the stop time // and only then look up the timer and call stop with the given time. ! long stopTime = System.currentTimeMillis(); ExecutionTimer timer = get(name); if(timer == null){ --- 63,68 ---- //we want to be as precise as possible, so we first read off the stop time // and only then look up the timer and call stop with the given time. ! // long stopTime = System.currentTimeMillis(); ! long stopTime = System.nanoTime(); ExecutionTimer timer = get(name); if(timer == null){ *************** *** 69,73 **** return -1; } ! return timer.stop(); } --- 70,75 ---- return -1; } ! timer.stop(stopTime); ! return timer.getLastEventTime(); } *************** *** 106,110 **** log.warn("Timer " + name + " doesn't exist, so can't increment it."); } ! timer.incTime(ms); } --- 108,112 ---- log.warn("Timer " + name + " doesn't exist, so can't increment it."); } ! timer.addTime(ms); } *************** *** 130,144 **** public String toString(){ ! ArrayList keys = new ArrayList(map.keySet()); Collections.sort(keys); int maxLength = 0; ! for(Object obj : keys){ ! String key = (String)obj; maxLength = (key.length() > maxLength) ? key.length() : maxLength; } StringBuffer sb = new StringBuffer(); String format = "%"+maxLength +"s = %s%n"; ! for(Object obj : keys){ ! String key = (String)obj; sb.append(String.format(format, key, get(key))); } --- 132,144 ---- public String toString(){ ! ArrayList<String> keys = new ArrayList<String>(map.keySet()); Collections.sort(keys); int maxLength = 0; ! for(String key : keys){ maxLength = (key.length() > maxLength) ? key.length() : maxLength; } StringBuffer sb = new StringBuffer(); String format = "%"+maxLength +"s = %s%n"; ! for(String key : keys){ sb.append(String.format(format, key, get(key))); } *************** *** 146,153 **** } ! public static long sumTimes(String name, Collection timings){ long totalTimeMS = 0; ! for(Object obj : timings){ ! Timing t = (Timing) obj; totalTimeMS += t.getElapsedTime(name); } --- 146,152 ---- } ! public static long sumTimes(String name, Collection<Timing> timings){ long totalTimeMS = 0; ! for(Timing t : timings){ totalTimeMS += t.getElapsedTime(name); } *************** *** 155,164 **** } ! public static Timing sumTimings(Collection timings){ if(timings.size()==0) return new Timing(); ! Set keys = ((Timing)timings.iterator().next()).map.keySet(); Timing timing = new Timing(); ! for(Object k : keys){ ! String key = (String) k; long total = sumTimes(key, timings); timing.reset(key); --- 154,162 ---- } ! public static Timing sumTimings(Collection<Timing> timings){ if(timings.size()==0) return new Timing(); ! Set<String> keys = (timings.iterator().next()).map.keySet(); Timing timing = new Timing(); ! for(String key : keys){ long total = sumTimes(key, timings); timing.reset(key); *************** *** 190,198 **** Timing.t.incTime(name[i], time[i]); } ! System.out.println(Timing.t); ! } ! ! ! ! } --- 188,192 ---- Timing.t.incTime(name[i], time[i]); } ! System.out.println(Timing.t); } ! } \ No newline at end of file |