-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopwatch_Nano.java
More file actions
42 lines (39 loc) · 944 Bytes
/
Stopwatch_Nano.java
File metadata and controls
42 lines (39 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* A class to help benchmark code
* It simulates a real stop watch
*/
public class Stopwatch_Nano {
private long startTime = -1;
private long stopTime = -1;
private boolean running = false;
public Stopwatch_Nano start() {
startTime = System.nanoTime();
running = true;
return this;
}
public Stopwatch_Nano stop() {
stopTime = System.nanoTime();
running = false;
return this;
}
/** returns elapsed time in Nanoseconds
* if the watch has never been started then
* return zero
*/
public long getElapsedTime() {
if (startTime == -1) {
return 0;
}
if (running){
return System.nanoTime() - startTime;
} else {
return stopTime-startTime;
}
}
public Stopwatch_Nano reset() {
startTime = -1;
stopTime = -1;
running = false;
return this;
}
}