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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
package shared;
import java.io.PrintStream;
public class Timer {
public Timer(){this(System.err, true);}
public Timer(String s){
this(System.err, true);
if(outstream!=null){outstream.println(s);}
}
public Timer(PrintStream outstream_){
this(outstream_, true);
}
public Timer(PrintStream outstream_, boolean addTab_){
outstream=outstream_;
addTab=addTab_;
start();
}
public long start(String s){
if(outstream!=null){outstream.println(s);}
return start();
}
public long stopAndPrint(){
long x=stop();
if(outstream!=null){outstream.println(this);}
return x;
}
public long stop(String s){
long x=stop();
if(addTab && s!=null && !s.endsWith("\t")){s=s+"\t";}
if(outstream!=null){outstream.println(s+this);}
return x;
}
public long stopAndStart(String s){
long x=stop();
if(addTab && s!=null && !s.endsWith("\t")){s=s+"\t";}
if(outstream!=null){outstream.println(s+this);}
start();
return x;
}
public long start(){
time1=time2=System.nanoTime();
elapsed=0;
return time1;
}
public long stop(){
time2=System.nanoTime();
elapsed=time2-time1;
return time2;
}
@Override
public String toString(){
// new Exception().printStackTrace();
return timeInSeconds(3)+" seconds.";
}
public String timeInSeconds(int decimals) {
return Tools.format("%."+decimals+"f", timeInSeconds());
}
public double timeInSeconds() {
return elapsed/1000000000d;
}
public long time1;
public long time2;
/** in nanos */
public long elapsed;
public PrintStream outstream=System.err;
public boolean addTab=true;
}
|