net.sf.antcontrib.perf

Class StopWatch

public class StopWatch extends Object

A stopwatch, useful for 'quick and dirty' performance testing. Typical usage:
 StopWatch sw = new StopWatch();  // automatically starts
 // do something here...
 sw.stop();
 System.out.println(sw.toString());   // print the total
 sw.start();  // restart the stopwatch
 // do some more things...
 sw.stop();
 System.out.println(sw.format(sw.elapsed()); // print the time since the last start
 System.out.println(sw.toString()); // print the cumulative total
 

Developed for use with Antelope, migrated to ant-contrib Oct 2003.

Version: $Revision: 1.3 $

Author: Dale Anson

Constructor Summary
StopWatch()
Starts the stopwatch.
StopWatch(String name)
Starts the stopwatch.
Method Summary
longelapsed()
Elapsed time, difference between the last start time and now.
Stringformat(long ms)
Formats the given time into decimal seconds.
StringgetName()
static voidmain(String[] args)
longstart()
Starts/restarts the stopwatch.
longstop()
Stops the stopwatch.
StringtoString()
Returns the total elapsed time of the stopwatch formatted in decimal seconds.
longtotal()
Total cumulative elapsed time.

Constructor Detail

StopWatch

public StopWatch()
Starts the stopwatch.

StopWatch

public StopWatch(String name)
Starts the stopwatch.

Parameters: name an identifying name for this StopWatch

Method Detail

elapsed

public long elapsed()
Elapsed time, difference between the last start time and now.

Returns: the elapsed time

format

public String format(long ms)
Formats the given time into decimal seconds.

Returns: the time formatted as mm:ss.ddd

getName

public String getName()

Returns: the name of this StopWatch

main

public static void main(String[] args)

start

public long start()
Starts/restarts the stopwatch. stop must be called prior to restart.

Returns: the start time, the long returned System.currentTimeMillis().

stop

public long stop()
Stops the stopwatch.

Returns: the stop time, the long returned System.currentTimeMillis().

toString

public String toString()
Returns the total elapsed time of the stopwatch formatted in decimal seconds.

Returns: [name: mm:ss.ddd]

total

public long total()
Total cumulative elapsed time.

Returns: the total time