-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.java
34 lines (27 loc) · 924 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
/** Timer class for roughly calculating running time of programs
* @author rbk
* Usage: Timer timer = new Timer();
* timer.start();
* timer.end();
* System.out.println(timer); // output statistics
*/
package sxs169430;
public class Timer {
long startTime, endTime, elapsedTime, memAvailable, memUsed;
public Timer() {
startTime = System.currentTimeMillis();
}
public void start() {
startTime = System.currentTimeMillis();
}
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.";
}
}