jpayne@68: package shared; jpayne@68: jpayne@68: import java.io.PrintStream; jpayne@68: import java.util.Locale; jpayne@68: jpayne@68: public class Timer { jpayne@68: jpayne@68: public Timer(){this(System.err, true);} jpayne@68: jpayne@68: public Timer(String s){ jpayne@68: this(System.err, true); jpayne@68: if(outstream!=null){outstream.println(s);} jpayne@68: } jpayne@68: jpayne@68: public Timer(PrintStream outstream_, boolean addTab_){ jpayne@68: outstream=outstream_; jpayne@68: addTab=addTab_; jpayne@68: start(); jpayne@68: } jpayne@68: jpayne@68: public long start(String s){ jpayne@68: if(outstream!=null){outstream.println(s);} jpayne@68: return start(); jpayne@68: } jpayne@68: jpayne@68: public long stopAndPrint(){ jpayne@68: long x=stop(); jpayne@68: if(outstream!=null){outstream.println(this);} jpayne@68: return x; jpayne@68: } jpayne@68: jpayne@68: public long stop(String s){ jpayne@68: long x=stop(); jpayne@68: if(addTab && s!=null && !s.endsWith("\t")){s=s+"\t";} jpayne@68: if(outstream!=null){outstream.println(s+this);} jpayne@68: return x; jpayne@68: } jpayne@68: jpayne@68: public long start(){ jpayne@68: time1=time2=System.nanoTime(); jpayne@68: elapsed=0; jpayne@68: return time1; jpayne@68: } jpayne@68: jpayne@68: public long stop(){ jpayne@68: time2=System.nanoTime(); jpayne@68: elapsed=time2-time1; jpayne@68: return time2; jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public String toString(){ jpayne@68: return timeInSeconds(3)+" seconds."; jpayne@68: } jpayne@68: jpayne@68: public String timeInSeconds(int decimals) { jpayne@68: return String.format(Locale.ROOT, "%."+decimals+"f", timeInSeconds()); jpayne@68: } jpayne@68: jpayne@68: public double timeInSeconds() { jpayne@68: return elapsed/1000000000d; jpayne@68: } jpayne@68: jpayne@68: public long time1; jpayne@68: public long time2; jpayne@68: /** in nanos */ jpayne@68: public long elapsed; jpayne@68: jpayne@68: public PrintStream outstream=System.err; jpayne@68: public boolean addTab=true; jpayne@68: }