martin.common
Class Stopwatch

java.lang.Object
  extended by martin.common.Stopwatch

public class Stopwatch
extends java.lang.Object

Class useful for profiling the time usage of java software. Consecutive calls using the same label are added together to a total sum. Example: Stopwatch s = new Stopwatch("Method 1"); method1(); s.stop(); Stopwatch.printStatistics();

Author:
Martin

Field Summary
private static java.util.Map<java.lang.String,java.lang.Integer> hits
           
private  java.lang.String label
           
private  long startTime
           
private static java.util.Map<java.lang.String,java.lang.Long> sums
           
private static java.util.concurrent.Semaphore totalSem
           
 
Constructor Summary
Stopwatch(java.lang.String label)
          Returns a running Stopwatch object.
 
Method Summary
 void cancel()
          Stops the stopwatch, without adding the elapsed time to the global data store.
static void printStats()
           
 void start()
          Starts the stopwatch, storing the time when this function is called.
static Stopwatch startNew(java.lang.String label)
          Starts the stopwatch, storing the time when this function is called.
 long stop()
          Stops the Stopwatch, and adds the elapsed time to a global sum for the label specified on construction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sums

private static java.util.Map<java.lang.String,java.lang.Long> sums

hits

private static java.util.Map<java.lang.String,java.lang.Integer> hits

totalSem

private static java.util.concurrent.Semaphore totalSem

startTime

private long startTime

label

private java.lang.String label
Constructor Detail

Stopwatch

public Stopwatch(java.lang.String label)
Returns a running Stopwatch object.

Parameters:
label -
Method Detail

startNew

public static Stopwatch startNew(java.lang.String label)
Starts the stopwatch, storing the time when this function is called.

Returns:
a reference to the stopwatch

start

public void start()
Starts the stopwatch, storing the time when this function is called.


cancel

public void cancel()
Stops the stopwatch, without adding the elapsed time to the global data store.


stop

public long stop()
Stops the Stopwatch, and adds the elapsed time to a global sum for the label specified on construction.

Returns:
the number of milliseconds elapsed since this stopwatch was last started.

printStats

public static void printStats()