com.svincent.util
Class Stopwatch

java.lang.Object
  |
  +--com.svincent.util.Stopwatch

public class Stopwatch
extends java.lang.Object

A timer class, useful for timing code.

I got tired of trying to figure out the Date class again and again, and having unpleasant output, because I was too lazy to process the output. But never again! Thanks to the wonders of Object Oriented Computing, I've written a Stopwatch class, which does all the work for me!

Using the Stopwatch is very easy. There are only two interesting methods: reset and getMillis.

reset resets the stopwatch to zero. Note that a newly constructed Stopwatch is implicitly reset.

getMillis returns the number of milliseconds that passed since the last call to reset. Note that printing a Stopwatch prints a human-readable representation of the milliseconds which have passed (in hh:mm:ss.mmm format).

The following code block illustrates use of the Stopwatch.

Author:
Shawn Vincent

Constructor Summary
Stopwatch()
           
 
Method Summary
 long getMillis()
          Return the number of milliseconds which have passed since we reset the stopwatch.
static void main(java.lang.String[] argv)
          Some test code.
 void reset()
          Resets the time to 0.
 java.lang.String tag()
          Returns a string representation of the current time.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stopwatch

public Stopwatch()
Method Detail

reset

public void reset()
Resets the time to 0.

(happens implicitly upon construction)


tag

public java.lang.String tag()
Returns a string representation of the current time.

This representation is in hh:mm:ss.mmm format, where hh == hours, mm == minutes, ss == seconds, and mmm=milliseconds. The hours field can be longer than two digits, if the Stopwatch has run for longer than 99 hours. (This is unlikely to be the case, however)

Returns:
The string representation of this object.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getMillis

public long getMillis()
Return the number of milliseconds which have passed since we reset the stopwatch.
Returns:
The number of milliseconds since the last call to reset

main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Some test code.

  1. argv[0] is parsed into an int.
  2. The current thread is asked to wait this long, in milliseconds.
  3. This waiting is timed by a Stopwatch instance, and the timing is printed to Util.out.
Parameters:
argv - The command line arguments.



This is documentation for Moksa Prolog, which can be found at http://www.svincent.com/moksa/

Copyright © 1999 Shawn P. Vincent.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.