You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/03/01 22:18:18 UTC
svn commit: r749114 -
/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java
Author: bayard
Date: Sun Mar 1 21:18:18 2009
New Revision: 749114
URL: http://svn.apache.org/viewvc?rev=749114&view=rev
Log:
Applying my patch from LANG-478 - moving StopWatch to using nanoTime under the hood.
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java?rev=749114&r1=749113&r2=749114&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java Sun Mar 1 21:18:18 2009
@@ -56,6 +56,8 @@
*/
public class StopWatch {
+ private static final long NANO_2_MILLIS = 1000000L;
+
// running states
private static final int STATE_UNSTARTED = 0;
@@ -86,6 +88,13 @@
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.
*/
private long stopTime;
@@ -118,7 +127,8 @@
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 @@
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 @@
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 @@
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 @@
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 @@
* @return the time in milliseconds
*/
public long getTime() {
+ return getNanoTime() / NANO_2_MILLIS;
+ }
+ /**
+ * <p>
+ * Get the time on the stopwatch in nanoseconds.
+ * </p>
+ *
+ * <p>
+ * This is either the time between the start and the moment this method is called, or the amount of time between
+ * start and stop.
+ * </p>
+ *
+ * @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 @@
* @since 2.1
*/
public long getSplitTime() {
+ return getSplitNanoTime() / NANO_2_MILLIS;
+ }
+ /**
+ * <p>
+ * Get the split time on the stopwatch in nanoseconds.
+ * </p>
+ *
+ * <p>
+ * This is the time between start and latest split.
+ * </p>
+ *
+ * @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 @@
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;
}
/**