Version 4.0.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();

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
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() and toValue() 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") ;


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 for logging.
 long toValue()
          Return the current "reading" on the stopwatch in milliseconds, 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 for logging.

Example return value : '1089 ms', which indicates just over a second.

Overrides:
toString in class Object

toValue

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


Version 4.0.0

Copyright Hirondelle Systems. Published December 21, 2008 - User Guide - All Docs.