001package stdlib;
002/* ***********************************************************************
003 *  Compilation:  javac Stopwatch.java
004 *
005 *
006 *************************************************************************/
007
008/**
009 *  <i>Stopwatch</i>. This class is a data type for measuring
010 *  the running time (wall clock) of a program.
011 *  <p>
012 *  For additional documentation, see
013 *  <a href="http://introcs.cs.princeton.edu/32class">Section 3.2</a> of
014 *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
015 *  by Robert Sedgewick and Kevin Wayne.
016 */
017
018
019
020public class Stopwatch {
021
022        private final long start;
023
024        /**
025         * Create a stopwatch object.
026         */
027        public Stopwatch() {
028                start = System.currentTimeMillis();
029        }
030
031
032        /**
033         * Return elapsed time (in seconds) since this object was created.
034         */
035        public double elapsedTime() {
036                long now = System.currentTimeMillis();
037                return (now - start) / 1000.0;
038        }
039
040}