programming-examples/java/Data_Structures/Stopwatch.java

42 lines
892 B
Java
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*************************************************************************
* Compilation: javac Stopwatch.java
*
*
*************************************************************************/
/**
* The Stopwatch 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;
/**
* Initialize a stopwatch object.
*/
public Stopwatch() {
start = System.currentTimeMillis();
}
/**
* Returns the elapsed time (in seconds) since this object was created.
*/
public double elapsedTime() {
long now = System.currentTimeMillis();
return (now - start) / 1000.0;
}
}