2011-10-30 05:38:17 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-11-18 04:13:38 +00:00
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.text.DecimalFormatSymbols;
|
2011-10-30 05:38:17 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2011-11-18 04:13:38 +00:00
|
|
|
import java.util.Locale;
|
2011-10-30 05:38:17 +00:00
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class ExecuteTimer {
|
|
|
|
private final transient List<ExecuteRecord> times;
|
|
|
|
private final transient DecimalFormat decimalFormat = new DecimalFormat("#0.000", DecimalFormatSymbols.getInstance(Locale.US));
|
2011-11-18 04:13:38 +00:00
|
|
|
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public ExecuteTimer() {
|
|
|
|
times = new ArrayList<ExecuteRecord>();
|
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public void start() {
|
|
|
|
times.clear();
|
|
|
|
mark("start");
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public void mark(final String label) {
|
|
|
|
if (!times.isEmpty() || "start".equals(label)) {
|
|
|
|
times.add(new ExecuteRecord(label, System.nanoTime()));
|
|
|
|
}
|
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public String end() {
|
|
|
|
final StringBuilder output = new StringBuilder();
|
|
|
|
output.append("execution time: ");
|
|
|
|
String mark;
|
|
|
|
long time0 = 0;
|
|
|
|
long time1 = 0;
|
|
|
|
long time2 = 0;
|
|
|
|
double duration;
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
for (ExecuteRecord pair : times) {
|
2015-06-03 20:11:56 +00:00
|
|
|
mark = pair.getMark();
|
|
|
|
time2 = pair.getTime();
|
2015-04-15 04:06:16 +00:00
|
|
|
if (time1 > 0) {
|
|
|
|
duration = (time2 - time1) / 1000000.0;
|
|
|
|
output.append(mark).append(": ").append(decimalFormat.format(duration)).append("ms - ");
|
|
|
|
} else {
|
|
|
|
time0 = time2;
|
|
|
|
}
|
|
|
|
time1 = time2;
|
|
|
|
}
|
|
|
|
duration = (time1 - time0) / 1000000.0;
|
|
|
|
output.append("Total: ").append(decimalFormat.format(duration)).append("ms");
|
|
|
|
times.clear();
|
|
|
|
return output.toString();
|
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
private static class ExecuteRecord {
|
|
|
|
private final String mark;
|
|
|
|
private final long time;
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public ExecuteRecord(final String mark, final long time) {
|
|
|
|
this.mark = mark;
|
|
|
|
this.time = time;
|
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public String getMark() {
|
|
|
|
return mark;
|
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public long getTime() {
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
}
|
2011-10-30 05:38:17 +00:00
|
|
|
}
|