Version 4.10.0

hirondelle.web4j.util
Class Stopwatch

Object
  extended by hirondelle.web4j.util.Stopwatch

public final class Stopwatch
extends Object

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

Stopwatch

public Stopwatch()
Method Detail

start

public void start()
Start the stopwatch.

You cannot call this method if the stopwatch is already started.


stop

public void stop()
Stop the stopwatch.

You can only call this method if the stopwatch has been started.


toString

public String toString()
Return the current "reading" on the stopwatch, in milliseconds, in a format suitable typical use cases.

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

Overrides:
toString in class Object

toValue

public long toValue()
Return the current "reading" on the stopwatch in raw nanoseconds, as a numeric type.


Version 4.10.0

Copyright Hirondelle Systems. Published October 19, 2013 - User Guide - All Docs.