01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2002 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package nice.tools.util;
12:
13: import java.util.*;
14:
15: /**
16: Tool to mesure time cumulatively. Used for benchmarking.
17:
18: @version $Date: 2002/08/24 23:51:36 $
19: @author Daniel Bonniot (bonniot@users.sourceforge.net)
20: */
21:
22: public class Chronometer {
23: public static Chronometer make(String name) {
24: Chronometer res = new Chronometer(name);
25: chronometers.add(res);
26: return res;
27: }
28:
29: public static void printAll() {
30: for (Iterator i = chronometers.iterator(); i.hasNext();)
31: java.lang.System.out.println(((Chronometer) i.next())
32: .toString());
33: }
34:
35: private static List chronometers = new LinkedList();
36:
37: private Chronometer(String name) {
38: this .name = name;
39: }
40:
41: public String toString() {
42: return name + ": " + getTotalTime() + " ms";
43: }
44:
45: private String name;
46:
47: private long total = 0;
48: private long startTime = -1;
49: private long stopTime = -1;
50: private boolean running = false;
51:
52: public void start() {
53: if (running)
54: throw new Error("Already running");
55: running = true;
56: startTime = java.lang.System.currentTimeMillis();
57: }
58:
59: public void stop() {
60: stopTime = java.lang.System.currentTimeMillis();
61: if (!running)
62: throw new Error("Not running");
63: running = false;
64: total += stopTime - startTime;
65: }
66:
67: /** returns elapsed time in milliseconds
68: * if the watch has never been started then
69: * return zero
70: */
71: public long getTotalTime() {
72: if (running)
73: throw new Error("Running");
74: return total;
75: }
76: }
|