-
Notifications
You must be signed in to change notification settings - Fork 2
/
Timer.java
33 lines (26 loc) · 955 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
/** Timer class for roughly calculating running time of programs
* @author Badhrinath Santhanam, Indhumathi Suryanarayanan, Karthik Ramakrishnan, Nandita Balasubramanian
* Usage: Timer timer = new Timer();
* timer.start();
* timer.end();
* System.out.println(timer); // output statistics
*/
public class Timer {
long startTime, endTime, elapsedTime, memAvailable, memUsed;
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/1000000) + " MB / " + (memAvailable/1000000) + " MB.";
}
}