|
Version 4.10.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Object hirondelle.web4j.util.Stopwatch
public final class Stopwatch
Allows timing of the execution of any block of code. Example use case:
Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); //..perform operations stopwatch.stop(); //timed in nanos, but toString is expressed in millis, //to three decimal places, to reflect the real resolution of most systems: System.out.println("The reading on the stopwatch is: " + stopwatch); //reuse the same stopwatch again //Note that there is no need to call a reset method. stopwatch.start(); //..perform operations stopwatch.stop(); //perform a numeric comparison, using raw nanoseconds: if ( stopwatch.toValue() > 5 ) { System.out.println("The reading is high: " + stopwatch); }
The value on the stopwatch may be inspected at any time using the
toString()
(millis) and toValue()
(nanos) methods.
Example 2
To time the various steps in a long startup or initialization task, your code may take the form:
Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); //..perform operation 1 log("Step 1 completed " + stopwatch + " after start."); //perform operation 2 log("Step 2 completed " + stopwatch + " after start."); //perform the last operation, operation 3 stopwatch.stop(); log("Final Step 3 completed " + stopwatch + " after start") ;
Implementation Note:
This class uses System.nanoTime()
, not System.currentTimeMillis()
, to
perform its timing operations.
Constructor Summary | |
---|---|
Stopwatch()
|
Method Summary | |
---|---|
void |
start()
Start the stopwatch. |
void |
stop()
Stop the stopwatch. |
String |
toString()
Return the current "reading" on the stopwatch, in milliseconds, in a format suitable typical use cases. |
long |
toValue()
Return the current "reading" on the stopwatch in raw nanoseconds, as a numeric type. |
Methods inherited from class Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Stopwatch()
Method Detail |
---|
public void start()
You cannot call this method if the stopwatch is already started.
public void stop()
You can only call this method if the stopwatch has been started.
public String toString()
Example return value : '108.236 ms'. The underlying timing is in nanos,
but it's expressed here in millis, for two reasons: the resolution on most systems is
in the microsecond range, and the full 9 digits are less easy to reader. If you need
the raw nanos, just use toValue()
instead.
Ref: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
toString
in class Object
public long toValue()
|
Version 4.10.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |