-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTimer.java
38 lines (32 loc) · 934 Bytes
/
Timer.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
package rsn170330.sp07;
/**
* Timer class for roughly calculating running time of programs
* @author rbk
* @author Rahul Nalawade (rsn170330)
*
* Usage: Timer timer = new Timer(); timer.start(); timer.end();
* System.out.println(timer); // output statistics
*/
public class Timer {
long startTime, endTime, elapsedTime, memAvailable, memUsed;
public Timer() {
startTime = System.currentTimeMillis();
}
public void start() {
startTime = System.currentTimeMillis();
}
public void scale(int num) {
elapsedTime /= num;
}
public Timer end() {
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
memAvailable = Runtime.getRuntime().totalMemory();
memUsed = memAvailable - Runtime.getRuntime().freeMemory();
return this;
}
public String toString() {
return "Time: " + elapsedTime + " msec.\n" + "Memory: " + (memUsed / 1048576) +
" MB / " + (memAvailable / 1048576) + " MB.";
}
}