You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

package util;
import java.util.concurrent.TimeUnit;
/**
* Adapted from http://memorynotfound.com/calculating-elapsed-time-java/
*/
public class TimeWatch {
private long starts;
private TimeWatch() {
reset();
}
public static TimeWatch start() {
return new TimeWatch();
}
private TimeWatch reset() {
starts = System.nanoTime();
return this;
}
private long time() {
long ends = System.nanoTime();
return ends - starts;
}
private long time(TimeUnit unit) {
return unit.convert(time(), TimeUnit.NANOSECONDS);
}
private String toMinuteSeconds() {
return String.format("%d min, %d sec", time(TimeUnit.MINUTES),
time(TimeUnit.SECONDS) - time(TimeUnit.MINUTES));
}
public String toFullTime() {
long hours = time(TimeUnit.HOURS);
long minutes = time(TimeUnit.MINUTES) - TimeUnit.HOURS.toMinutes(hours);
long seconds = time(TimeUnit.SECONDS) - TimeUnit.HOURS.toSeconds(hours) - TimeUnit.MINUTES.toSeconds(minutes);
long milliseconds = time(TimeUnit.MILLISECONDS) - TimeUnit.HOURS.toMillis(hours) - TimeUnit.MINUTES.toMillis(minutes) - TimeUnit.SECONDS.toMillis(seconds);
return String.format("%d h, %d min, %d s, %d ms", hours, minutes, seconds, milliseconds);
}
public String toString() {
return "Elapsed Time in nano seconds: ";
}
}