fc.util
Class NanoWatch

java.lang.Object
  extended by fc.util.Watch
      extended by fc.util.NanoWatch

public class NanoWatch
extends Watch

Allows measuring the start and stop time and (hence the elapsed time) of some event. ("event" meaning something of interest, for example, a method call). Can be started/stopped repeatedly. (cumulative time across all such start/stops are available via the cumulativeTime() method).

All times are in nano-seconds.

Thread Safety: This class is not threadsafe and it's method do not acquire any locks to reduce any time skew due to lock acquisition. Multiple threads should use separate NanoWatch objects or alternatively, higher level synchronization.

See Also:
A lower precision (milliseconds) watch.

Constructor Summary
NanoWatch()
           
NanoWatch(String name)
           
 
Method Summary
 long cumulativeTime()
          Returns the total time recorded by this Watch (across several starts/stops)
 long getTime()
          Returns the time elapsed since the Watch was started.
 double getTimeInMillis()
          Returns the elapsed time in milliseconds.
 boolean isRunning()
          Is the Watch currently running ?
static void main(String[] args)
           
 void reset()
          Reset all values to zero.
 NanoWatch start()
          Start measuring time.
 void stop()
          Stop measuring the time
 long time()
          Returns the time elapsed since the Watch was started.
 String toString()
          Describes the current state of this watch.
 
Methods inherited from class fc.util.Watch
getTimeInSeconds, restart, timeInSeconds
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NanoWatch

public NanoWatch(String name)

NanoWatch

public NanoWatch()
Method Detail

start

public NanoWatch start()
Start measuring time. Call this method just before calling the method/code to be instrumented. This method does not reset the Watch, so the reset method should be called before calling this method again.

Overrides:
start in class Watch

stop

public void stop()
Stop measuring the time

Overrides:
stop in class Watch

time

public long time()
Returns the time elapsed since the Watch was started. If the watch was started and stopped, returns the time between the start/stop interval.

Overrides:
time in class Watch
Throws:
RuntimeException - if the watch was never started before calling this method.

getTime

public long getTime()
Returns the time elapsed since the Watch was started. (this method is an alias for time() method).

Overrides:
getTime in class Watch

getTimeInMillis

public double getTimeInMillis()
Returns the elapsed time in milliseconds. Useful when nanosecond values are too hard to read.

Overrides:
getTimeInMillis in class Watch

cumulativeTime

public long cumulativeTime()
Returns the total time recorded by this Watch (across several starts/stops)

Overrides:
cumulativeTime in class Watch

reset

public void reset()
Reset all values to zero. This method should be called before this object is used again.

Overrides:
reset in class Watch

isRunning

public boolean isRunning()
Is the Watch currently running ?

Overrides:
isRunning in class Watch

toString

public String toString()
Description copied from class: Watch
Describes the current state of this watch. The exact details of said description are unspecified and subject to change.

Overrides:
toString in class Watch

main

public static void main(String[] args)