| 1 |
package ml; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
public class Profiler{ |
| 6 |
|
| 7 |
public Profiler(String prefix_, int len_){ |
| 8 |
prefix=prefix_; |
| 9 |
times=new long[len_]; |
| 10 |
reset(); |
| 11 |
} |
| 12 |
|
| 13 |
void log() { |
| 14 |
if(!PROFILING){return;} |
| 15 |
log(idx); |
| 16 |
idx++; |
| 17 |
} |
| 18 |
|
| 19 |
void log(int idx) { |
| 20 |
long nanos=System.nanoTime(); |
| 21 |
long dif=nanos-start; |
| 22 |
times[idx]+=dif; |
| 23 |
start=nanos; |
| 24 |
} |
| 25 |
|
| 26 |
public String toString() { |
| 27 |
StringBuilder sb=new StringBuilder(); |
| 28 |
sb.append(prefix); |
| 29 |
for(int i=0; i<times.length; i++) { |
| 30 |
|
| 31 |
sb.append('\t').append(String.format("%d", times[i]/1000000)); |
| 32 |
} |
| 33 |
return sb.toString(); |
| 34 |
} |
| 35 |
|
| 36 |
void printTimes() { |
| 37 |
if(PROFILING) { |
| 38 |
System.err.println(this); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
void accumulate(Profiler p) { |
| 43 |
for(int i=0; i<times.length; i++) { |
| 44 |
times[i]+=p.times[i]; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
void reset() { |
| 49 |
start=System.nanoTime(); |
| 50 |
idx=0; |
| 51 |
} |
| 52 |
|
| 53 |
void clear() { |
| 54 |
Arrays.fill(times, 0); |
| 55 |
reset(); |
| 56 |
} |
| 57 |
|
| 58 |
private long start; |
| 59 |
private int idx=0; |
| 60 |
final long[] times; |
| 61 |
final String prefix; |
| 62 |
|
| 63 |
public static boolean PROFILING=false; |
| 64 |
} |