001: package jimm.util;
002:
003: import java.io.PrintWriter;
004:
005: /**
006: * Prints time durations; used for development purposes only.
007: * <p>
008: * No threads or system resources are harmed in the making of a stop watch. A
009: * stop watch simply remembers the start time (and elapsed time when paused)
010: * and prints the total "running" time when either {@link #mark} or
011: * {@link #stop} is called.
012: * <p>
013: * {@link #stop} doesn't really stop anything. You can call stop as many times
014: * as you like and it will print the time elapsed since start. It would be
015: * easy to change this behavior, but I haven't needed to.
016: *
017: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018: */
019: public class StopWatch {
020:
021: protected String name;
022: protected long t0;
023: protected long elapsedTime;
024: protected PrintWriter out;
025:
026: public StopWatch() {
027: this (null, null);
028: }
029:
030: public StopWatch(String name) {
031: this (name, null);
032: }
033:
034: public StopWatch(PrintWriter out) {
035: this (null, out);
036: }
037:
038: public StopWatch(String name, PrintWriter out) {
039: this .name = name;
040: if (out == null)
041: this .out = new PrintWriter(System.err);
042: else
043: this .out = out;
044: elapsedTime = -1L; // So we can tell if we are ever started
045: }
046:
047: /**
048: * Remembers the current time and prints a message.
049: */
050: public void start() {
051: start(true);
052: }
053:
054: /**
055: * Remembers the current time and prints a message if requested.
056: *
057: * @param printStarting if <code>true</code> and this stop watch has a
058: * name, print a message
059: */
060: public void start(boolean printStarting) {
061: if (t0 != 0)
062: System.err
063: .println("(warning: StopWatch already started; resetting)");
064: if (printStarting && name != null)
065: System.err.println("starting " + name);
066: elapsedTime = 0;
067: t0 = System.currentTimeMillis();
068: }
069:
070: /**
071: * Pauses the stop watch.
072: */
073: public void pause() {
074: long now = System.currentTimeMillis();
075: elapsedTime += now - t0;
076: t0 = 0;
077: }
078:
079: /**
080: * Resumes the stop watch.
081: */
082: public void resume() {
083: t0 = System.currentTimeMillis();
084: }
085:
086: /**
087: * Prints the current elapsed time without stopping.
088: */
089: public void mark() {
090: stop(null, true);
091: }
092:
093: /**
094: * Prints the current elapsed time without stopping, along with the
095: * stop watch name if <var>printMark</var> is <code>true</code>.
096: *
097: * @param printMark if <code>true</code>, the stop watch name will
098: * be printed
099: */
100: public void mark(boolean printMark) {
101: stop(null, printMark);
102: }
103:
104: /**
105: * Prints the current elapsed time without stopping, along with the
106: * stop watch name and <var>msg</var>.
107: *
108: * @param msg a message to print
109: */
110: public void mark(String msg) {
111: stop(msg, true);
112: }
113:
114: /**
115: * Prints the current elapsed time without stopping, along with, along with
116: * the stop watch name if <var>printMark</var> is <code>true</code> and the
117: * <var>msg</var> if it's not <code>null</code>.
118: *
119: * @param msg a message to print
120: * @param printMark if <code>true</code>, the stop watch name will
121: * be printed
122: */
123: public void mark(String msg, boolean printMark) {
124: stop(msg, printMark);
125: }
126:
127: /**
128: * Stops the stop watch and prints the name of this stop watch and the current
129: * elapsed time.
130: */
131: public void stop() {
132: stop(null, true);
133: }
134:
135: /**
136: * Stops the stop watch and prints the name of this stop watch,
137: * <var>msg</var> if non-<code>null</code>, and the current elapsed time.
138: *
139: * @param msg a message to print; may be <code>null</code>
140: */
141: public void stop(String msg) {
142: stop(msg, true);
143: }
144:
145: /**
146: * Prints the current elapsed time, along with the stop watch name if
147: * <var>printMark</var> is <code>true</code> and the <var>msg</var> if it's
148: * not <code>null</code>.
149: *
150: * @param msg a message to print; may be <code>null</code>
151: * @param printName if <code>true</code>, the stop watch name will
152: * be printed
153: */
154: public void stop(String msg, boolean printName) {
155: long now = System.currentTimeMillis();
156:
157: if (elapsedTime == -1) {
158: System.err.println("(StopWatch"
159: + (name != null ? (" \"" + name + '"') : "")
160: + " was stopped without ever being started)");
161: return;
162: }
163:
164: long total = elapsedTime;
165: if (t0 != 0)
166: total += now - t0;
167:
168: String separator = null;
169: if (printName && name != null) {
170: System.err.print(name);
171: separator = ": ";
172: }
173: if (msg != null) {
174: if (separator != null)
175: System.err.print(' ');
176: System.err.print("(" + msg + ")");
177: separator = ": ";
178: }
179: if (separator != null)
180: System.err.print(separator);
181:
182: System.err.println("" + (total / 1000.0) + " seconds");
183: }
184:
185: }
|