📄 stopwatch.java
字号:
/* * StopWatch.java * * Created on September 25, 2003, 1:53 PM */package cs101.util;/** * A stopwatch for timing the execution of code. This class is intended to * provide aproximate time of execution to a resolution of single digit * miliseconds on most modern machines. Estimates of cost on a dual * Athlon MP 2000+ machien yeild the following costs for the following methods * <ul> * <li><code>start()</code> takes aprox 0.000269 seconds * <li><code>stop()</code> takes aprox 0.000570 seconds * <li><code>reset()</code> takes aprox 0.000274 seconds * <li><code>elapsed()</code> takes aprox 0.000278 seconds (stopped) * <li><code>look()</code> takes aprox 0.006071 seconds(stopped) * <li><code>elapsed()</code> takes aprox 0.000285 seconds (running) * <li><code>look()</code> takes aprox 0.006209 seconds (running) * </ul> * * @author Gus Heck (gus.heck@olin.edu) * @version $Id: StopWatch.java,v 1.1 2003/10/16 20:40:51 gus Exp $ */public class StopWatch { public static long MS_SEC = 1000l; public static long MS_MIN = 60000l; public static long MS_HOUR = 3600000l; private long started; private long startedFrom; private long stopped; private boolean running; /** Creates a new instance of StopWatch. */ public StopWatch() { stop(); reset(); } /** * Start the stopwatch. */ public void start() { running = true; started = System.currentTimeMillis(); } /** * Stop the stopwatch. */ public void stop() { stopped = System.currentTimeMillis(); running = false; startedFrom = elapsed(); } /** * Reset the stopwatch. It is perfectly legal and reasonable to * reset the stopwatch while it is still running. The elapsed time on * the stopwatch is set to 0 regardless. */ public void reset() { stopped = started = System.currentTimeMillis(); startedFrom = 0; } public long elapsed() { long time = System.currentTimeMillis(); return ((running ? time : stopped) - started) + startedFrom; } public String look() { StringBuffer elapsedTime = new StringBuffer(); long time = elapsed(); long ms = time % MS_SEC; long sec = (time % MS_MIN)/MS_SEC; long min = (time % MS_HOUR)/MS_MIN; long hour = time/MS_HOUR; elapsedTime.append(hour + " hours "); elapsedTime.append(min + " minutes "); elapsedTime.append(sec + "." + ms + " seconds "); elapsedTime.append("have elapsed."); return elapsedTime.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -