-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNanoTimer.java
More file actions
44 lines (34 loc) · 843 Bytes
/
NanoTimer.java
File metadata and controls
44 lines (34 loc) · 843 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
43
44
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class NanoTimer {
private static final long NANOS_IN_SECONDS = 1000000000;
private long timeStart;
private long timeEnd;
public NanoTimer(){
reset();
}
public void start(){
timeStart = System.nanoTime();
}
public void stop(){
timeEnd = System.nanoTime();
}
public void reset(){
timeStart = 0;
timeEnd = 0;
}
public long getLapsed(){
return (timeEnd-timeStart);
}
public String getFormattedTimeLapsed(){
String toReturn = getLapsed() + " ns";
if(getLapsed() >= NANOS_IN_SECONDS / 1000){
DecimalFormat df = new DecimalFormat("#0.000");
double inSeconds = 1.0 * getLapsed() / NANOS_IN_SECONDS;
toReturn += " (" + df.format(inSeconds) + " sec)";
}else{
toReturn += " (< 0.001 sec)";
}
return toReturn;
}
}