Stopwatch.java


Below is the syntax highlighted version of Stopwatch.java from § Standard Libraries.   Here is the Javadoc.


/******************************************************************************
 *  Compilation:  javac Stopwatch.java
 *  Execution:    none
 *  Dependencies: none
 *
 *  A utility class to measure the running time (wall clock) of a
 *  program.
 *
 ******************************************************************************/

/**
 *  The <tt>Stopwatch</tt> data type is for measuring
 *  the time that elapses between the start and end of a
 *  programming task (wall-clock time).
 *
 *  See {@link StopwatchCPU} for a version that measures CPU time.
 *
 *  @author Robert Sedgewick
 *  @author Kevin Wayne
 */


public class Stopwatch { 

    private final long start;

    /**
     * Initializes a new stopwatch.
     */
    public Stopwatch() {
        start = System.currentTimeMillis();
    } 


    /**
     * Returns the elapsed CPU time (in seconds) since the stopwatch was created.
     *
     * @return elapsed CPU time (in seconds) since the stopwatch was created
     */
    public double elapsedTime() {
        long now = System.currentTimeMillis();
        return (now - start) / 1000.0;
    }

} 


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Aug 14 11:44:16 EDT 2015.