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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
package ml;
import java.util.Arrays;
public class Profiler{
public Profiler(String prefix_, int len_){
prefix=prefix_;
times=new long[len_];
reset();
}
void log() {
if(!PROFILING){return;}
log(idx);
idx++;
}
void log(int idx) {
long nanos=System.nanoTime();
long dif=nanos-start;
times[idx]+=dif;
start=nanos;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append(prefix);
for(int i=0; i<times.length; i++) {
// sb.append('\t').append(String.format("%.4f", array[i]/1000000.0));
sb.append('\t').append(String.format("%d", times[i]/1000000));
}
return sb.toString();
}
void printTimes() {
if(PROFILING) {
System.err.println(this);
}
}
void accumulate(Profiler p) {
for(int i=0; i<times.length; i++) {
times[i]+=p.times[i];
}
}
void reset() {
start=System.nanoTime();
idx=0;
}
void clear() {
Arrays.fill(times, 0);
reset();
}
private long start;
private int idx=0;
final long[] times;
final String prefix;
public static boolean PROFILING=false;//does not seem to impact speed
}
|