From cb8a0d43eade243f6bcfb391bb5865b0d49366e0 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sun, 1 Mar 2009 21:18:18 +0000 Subject: [PATCH] Applying my patch from LANG-478 - moving StopWatch to using nanoTime under the hood. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@749114 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/time/StopWatch.java | 59 ++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/commons/lang/time/StopWatch.java b/src/java/org/apache/commons/lang/time/StopWatch.java index 0dd3d021b..de9193ce6 100644 --- a/src/java/org/apache/commons/lang/time/StopWatch.java +++ b/src/java/org/apache/commons/lang/time/StopWatch.java @@ -56,6 +56,8 @@ */ public class StopWatch { + private static final long NANO_2_MILLIS = 1000000L; + // running states private static final int STATE_UNSTARTED = 0; @@ -85,6 +87,13 @@ public class StopWatch { */ private long startTime; + /** + * The start time in Millis - nanoTime is only for elapsed time so we + * need to also store the currentTimeMillis to maintain the old + * getStartTime API. + */ + private long startTimeMillis; + /** * The stop time. */ @@ -118,7 +127,8 @@ public void start() { if (this.runningState != STATE_UNSTARTED) { throw new IllegalStateException("Stopwatch already started. "); } - this.startTime = System.currentTimeMillis(); + this.startTime = System.nanoTime(); + this.startTimeMillis = System.currentTimeMillis(); this.runningState = STATE_RUNNING; } @@ -139,7 +149,7 @@ public void stop() { throw new IllegalStateException("Stopwatch is not running. "); } if (this.runningState == STATE_RUNNING) { - this.stopTime = System.currentTimeMillis(); + this.stopTime = System.nanoTime(); } this.runningState = STATE_STOPPED; } @@ -175,7 +185,7 @@ public void split() { if (this.runningState != STATE_RUNNING) { throw new IllegalStateException("Stopwatch is not running. "); } - this.stopTime = System.currentTimeMillis(); + this.stopTime = System.nanoTime(); this.splitState = STATE_SPLIT; } @@ -216,7 +226,7 @@ public void suspend() { if (this.runningState != STATE_RUNNING) { throw new IllegalStateException("Stopwatch must be running to suspend. "); } - this.stopTime = System.currentTimeMillis(); + this.stopTime = System.nanoTime(); this.runningState = STATE_SUSPENDED; } @@ -237,7 +247,7 @@ public void resume() { if (this.runningState != STATE_SUSPENDED) { throw new IllegalStateException("Stopwatch must be suspended to resume. "); } - this.startTime += (System.currentTimeMillis() - this.stopTime); + this.startTime += (System.nanoTime() - this.stopTime); this.runningState = STATE_RUNNING; } @@ -254,12 +264,28 @@ public void resume() { * @return the time in milliseconds */ public long getTime() { + return getNanoTime() / NANO_2_MILLIS; + } + /** + *

+ * Get the time on the stopwatch in nanoseconds. + *

+ * + *

+ * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. + *

+ * + * @return the time in nanoseconds + * @since 3.0 + */ + public long getNanoTime() { if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) { return this.stopTime - this.startTime; } else if (this.runningState == STATE_UNSTARTED) { return 0; } else if (this.runningState == STATE_RUNNING) { - return System.currentTimeMillis() - this.startTime; + return System.nanoTime() - this.startTime; } throw new RuntimeException("Illegal running state has occured. "); } @@ -280,6 +306,24 @@ public long getTime() { * @since 2.1 */ public long getSplitTime() { + return getSplitNanoTime() / NANO_2_MILLIS; + } + /** + *

+ * Get the split time on the stopwatch in nanoseconds. + *

+ * + *

+ * This is the time between start and latest split. + *

+ * + * @return the split time in nanoseconds + * + * @throws IllegalStateException + * if the StopWatch has not yet been split. + * @since 3.0 + */ + public long getSplitNanoTime() { if (this.splitState != STATE_SPLIT) { throw new IllegalStateException("Stopwatch must be split to get the split time. "); } @@ -298,7 +342,8 @@ public long getStartTime() { if (this.runningState == STATE_UNSTARTED) { throw new IllegalStateException("Stopwatch has not been started"); } - return this.startTime; + // System.nanoTime is for elapsed time + return this.startTimeMillis; } /**