Java Reference
In-Depth Information
38
39 /**
40
Returns the total elapsed time.
41 @return
the total elapsed time
42 */
43
public long
getElapsedTime()
44 {
45
if
(isRunning)
46 {
47
long
endTime =
System.currentTimeMillis() ;
48
return
elapsedTime + endTime -
startTime;
49 }
50
else
51
return
elapsedTime;
52 }
53
54 /**
55
Stops the watch and resets the elapsed time to 0.
56 */
57
public void
reset()
58 {
59 elapsedTime =
0
;
60 isRunning =
false
;
61 }
62
63
private long
elapsedTime;
64
private long
startTime;
65
private boolean
isRunning;
66 }
632
633
Here is how we will use the stopwatch to measure the performance of the sorting
algorithm:
ch14/selsort/SelectionSortTimer.java
1
import
java.util.Scanner;
2
3 /**
4
This program measures how long it takes to sort an
5
array of a user-specified size with the selection