-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChrono.java
87 lines (72 loc) · 1.53 KB
/
Chrono.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package tools;
/**
* The Chrono class
*
* You can use this class to time code easily. Result is computed in
* milliseconds (1 000 milliseconds = 1 second). Example:
* @note Give name to the Chrono help you to manage several
* chronos at the same time (if you store several chrono in ArrayList for example).
*/
public class Chrono {
private long startTime;
private long endTime;
private float duration;
private String name;
public Chrono() {
name = "default";
duration = 0;
}
public Chrono(String name) {
this.name = name;
duration = 0;
}
/**
* This method start the chrono
*/
public void start() {
startTime = System.currentTimeMillis();
}
/**
* This method stop the timer and store the result in duration
*/
public void stop(){
endTime = System.currentTimeMillis();
duration += endTime - startTime;
}
/**
* This method return computed time since chrono was started
* @return
*/
public float currentTime(){
endTime = System.currentTimeMillis();
return endTime - startTime;
}
/**
* Reset timer and set the time to now()
*/
public void reset() {
startTime = System.currentTimeMillis();
endTime = startTime;
duration = 0;
}
/**
* Resume timer
*/
public void resume() {
startTime = System.currentTimeMillis();
}
/**
*
* @return duration in microseconds
*/
public float getDuration() {
return duration;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Chrono [name=" + name +", duration=" + duration + "]";
}
}