Package edu.princeton.cs.algs4
Class Stopwatch
- Object
-
- edu.princeton.cs.algs4.Stopwatch
-
public class Stopwatch extends Object
TheStopwatchdata 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 aStopwatchobject, execute the code you want to time, and then call theelapsedTime()method to get the elapsed time in seconds.Stopwatch stopwatch = new Stopwatch(); // do some computationally intensive calculation here double time = stopwatch.elapsedTime();
See
StopwatchCPUfor a version that measures CPU time. For additional documentation, see Section 1.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description Stopwatch()Initializes a new stopwatch.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleelapsedTime()Returns the elapsed CPU time (in seconds) since the stopwatch was created.static voidmain(String[] args)Unit tests theStopwatchdata type.
-
-
-
Method Detail
-
elapsedTime
public double elapsedTime()
Returns the elapsed CPU time (in seconds) since the stopwatch was created.- Returns:
- elapsed CPU time (in seconds) since the stopwatch was created
-
main
public static void main(String[] args)
Unit tests theStopwatchdata type. Takes a command-line argumentnand computes the sum of the square roots of the firstnpositive integers, first usingMath.sqrt(), then usingMath.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).- Parameters:
args- the command-line arguments
-
-