com.ecyrd.speed4j
Class StopWatch

java.lang.Object
  extended by com.ecyrd.speed4j.StopWatch
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
LoggingStopWatch

public class StopWatch
extends Object
implements Serializable

Creates a simple StopWatch with nanosecond precision (though not necessarily accuracy).

A "tag" is an unique grouping identifier. A "message" can be anything you like; it just travels with the StopWatch and is output with the toString method.

Tags must not contain whitespace, forward slash or commas. All other characters are allowed.

Most of the StopWatch methods return a reference to itself for easy chaining.

See Also:
Serialized Form

Constructor Summary
StopWatch()
           
StopWatch(String tag)
           
StopWatch(String tag, String message)
           
 
Method Summary
 StopWatch freeze()
          Returns a cloned, freezed copy of the StopWatch.
 long getCreationTime()
          Returns the moment in time at which this StopWatch was created (milliseconds since EPOCH).
 String getMessage()
          Returns the message associated with this StopWatch.
 String getTag()
          Returns the tag (grouping) for this StopWatch.
 long getTimeNanos()
          Returns the elapsed time in nanoseconds.
protected  void internalStop()
          The internal stop() method, which can be overridden by subclasses to provide additional functionality at stop().
 StopWatch lap()
          Stops and starts the StopWatch, essentially resetting it.
 StopWatch start()
          Starts a StopWatch which has been previously stopped.
 StopWatch stop()
          Stops the StopWatch.
 StopWatch stop(String tag)
          Stops the StopWatch and assigns the given tag to it.
 StopWatch stop(String tag, String message)
          Stops the StopWatch, assigns the tag and a free-form message.
 String toString()
          Returns a human-readable string.
 String toString(int iterations)
          Returns a human readable string which also calculates the speed of a single operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StopWatch

public StopWatch()

StopWatch

public StopWatch(String tag)

StopWatch

public StopWatch(String tag,
                 String message)
Method Detail

start

public StopWatch start()
Starts a StopWatch which has been previously stopped. If the StopWatch was already running, this method will reset it.

Returns:
This StopWatch.

internalStop

protected void internalStop()
The internal stop() method, which can be overridden by subclasses to provide additional functionality at stop(). Don't forget to call super.stop() in your subclass, or else the clock will not be stopped.


stop

public StopWatch stop()
Stops the StopWatch.

Returns:
This StopWatch instance.

stop

public StopWatch stop(String tag)
Stops the StopWatch and assigns the given tag to it.

Parameters:
tag - The tag to assign.
Returns:
This StopWatch.

stop

public StopWatch stop(String tag,
                      String message)
Stops the StopWatch, assigns the tag and a free-form message.

Parameters:
tag - The tag to assign.
message - A free-form message that associates with this particular StopWatch.
Returns:
This StopWatch.

lap

public StopWatch lap()
Stops and starts the StopWatch, essentially resetting it.

Returns:
This StopWatch.

getMessage

public String getMessage()
Returns the message associated with this StopWatch.

Returns:
The message, or null, if no message has been associated.

getTag

public String getTag()
Returns the tag (grouping) for this StopWatch.

Returns:
The tag, or null, if no tag has yet been assigned.

getTimeNanos

public long getTimeNanos()
Returns the elapsed time in nanoseconds.

Returns:

getCreationTime

public long getCreationTime()
Returns the moment in time at which this StopWatch was created (milliseconds since EPOCH).

Returns:
Start time.

toString

public String toString()
Returns a human-readable string. This is a slowish op, so don't call unnecessarily. Do NOT rely this in being any particular format.

Overrides:
toString in class Object

toString

public String toString(int iterations)
Returns a human readable string which also calculates the speed of a single operation. Do NOT rely on this being in any particular format. For example:
    StopWatch sw = ...
    for( int i = 0; i < 1000; i++ )
    {
       // Do something
    }
    sw.stop("test");
    System.out.println( sw.toString(1000) );
  
This might print out something like:
    test: 14520 ms (68 iterations/second)
  

Parameters:
iterations -
Returns:

freeze

public StopWatch freeze()
Returns a cloned, freezed copy of the StopWatch. The returned StopWatch is automatically stopped.

Returns:


Copyright © 2011. All Rights Reserved.