public class Stopwatch extends ObjectThe
Stopwatchdata type is for measuring the time that elapses between the start and end of a programming task (wall-clock time). To measure the running time of a code fragment, construct a
Stopwatchobject, execute the code you want to time, and then call the
elapsedTime()method to get the elapsed time in seconds.
Stopwatch stopwatch = new Stopwatch(); // do some computationally intensive calculation here double time = stopwatch.elapsedTime();
- Robert Sedgewick, Kevin Wayne
Constructors Constructor Description
Stopwatch()Initializes a new stopwatch.
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description
elapsedTime()Returns the elapsed CPU time (in seconds) since the stopwatch was created.
main(String args)Unit tests the
public double elapsedTime()Returns the elapsed CPU time (in seconds) since the stopwatch was created.
- elapsed CPU time (in seconds) since the stopwatch was created
public static void main(String args)Unit tests the
Stopwatchdata type. Takes a command-line argument
nand computes the sum of the square roots of the first
npositive integers, first using
Math.sqrt(), then using
Math.pow(). It prints to standard output the sum and the amount of time to compute the sum. Note that the discrete sum can be approximated by an integral - the sum should be approximately 2/3 * (n^(3/2) - 1).
args- the command-line arguments