1 30 31 32 package org.hsqldb.lib; 33 34 47 public class StopWatch { 48 49 54 private long startTime; 55 private long lastStart; 56 57 61 private long total; 62 63 64 boolean running = false; 65 66 67 public StopWatch() { 68 this(true); 69 } 70 71 72 public StopWatch(boolean start) { 73 74 if (start) { 75 start(); 76 } 77 } 78 79 85 public long elapsedTime() { 86 87 if (running) { 88 return total + System.currentTimeMillis() - startTime; 89 } else { 90 return total; 91 } 92 } 93 94 100 public long currentElapsedTime() { 101 102 if (running) { 103 return System.currentTimeMillis() - startTime; 104 } else { 105 return 0; 106 } 107 } 108 109 110 public void zero() { 111 112 total = 0; 113 114 start(); 115 } 116 117 123 public void start() { 124 startTime = System.currentTimeMillis(); 125 running = true; 126 } 127 128 136 public void stop() { 137 138 if (running) { 139 total += System.currentTimeMillis() - startTime; 140 running = false; 141 } 142 } 143 144 public void mark() { 145 stop(); 146 start(); 147 } 148 149 154 public String elapsedTimeToMessage(String prefix) { 155 return prefix + " in " + elapsedTime() + " ms."; 156 } 157 158 163 public String currentElapsedTimeToMessage(String prefix) { 164 return prefix + " in " + currentElapsedTime() + " ms."; 165 } 166 167 183 public String toString() { 184 return super.toString() + "[running=" + running + ", startTime=" 185 + startTime + ", total=" + total + "]"; 186 } 187 } 188 | Popular Tags |