| 1 |
package shared; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
public class Timer { |
| 6 |
|
| 7 |
public Timer(){this(System.err, true);} |
| 8 |
|
| 9 |
public Timer(String s){ |
| 10 |
this(System.err, true); |
| 11 |
if(outstream!=null){outstream.println(s);} |
| 12 |
} |
| 13 |
|
| 14 |
public Timer(PrintStream outstream_){ |
| 15 |
this(outstream_, true); |
| 16 |
} |
| 17 |
|
| 18 |
public Timer(PrintStream outstream_, boolean addTab_){ |
| 19 |
outstream=outstream_; |
| 20 |
addTab=addTab_; |
| 21 |
start(); |
| 22 |
} |
| 23 |
|
| 24 |
public long start(String s){ |
| 25 |
if(outstream!=null){outstream.println(s);} |
| 26 |
return start(); |
| 27 |
} |
| 28 |
|
| 29 |
public long stopAndPrint(){ |
| 30 |
long x=stop(); |
| 31 |
if(outstream!=null){outstream.println(this);} |
| 32 |
return x; |
| 33 |
} |
| 34 |
|
| 35 |
public long stop(String s){ |
| 36 |
long x=stop(); |
| 37 |
if(addTab && s!=null && !s.endsWith("\t")){s=s+"\t";} |
| 38 |
if(outstream!=null){outstream.println(s+this);} |
| 39 |
return x; |
| 40 |
} |
| 41 |
|
| 42 |
public long stopAndStart(String s){ |
| 43 |
long x=stop(); |
| 44 |
if(addTab && s!=null && !s.endsWith("\t")){s=s+"\t";} |
| 45 |
if(outstream!=null){outstream.println(s+this);} |
| 46 |
start(); |
| 47 |
return x; |
| 48 |
} |
| 49 |
|
| 50 |
public long start(){ |
| 51 |
time1=time2=System.nanoTime(); |
| 52 |
elapsed=0; |
| 53 |
return time1; |
| 54 |
} |
| 55 |
|
| 56 |
public long stop(){ |
| 57 |
time2=System.nanoTime(); |
| 58 |
elapsed=time2-time1; |
| 59 |
return time2; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public String toString(){ |
| 64 |
|
| 65 |
return timeInSeconds(3)+" seconds."; |
| 66 |
} |
| 67 |
|
| 68 |
public String timeInSeconds(int decimals) { |
| 69 |
return Tools.format("%."+decimals+"f", timeInSeconds()); |
| 70 |
} |
| 71 |
|
| 72 |
public double timeInSeconds() { |
| 73 |
return elapsed/1000000000d; |
| 74 |
} |
| 75 |
|
| 76 |
public long time1; |
| 77 |
public long time2; |
| 78 |
|
| 79 |
public long elapsed; |
| 80 |
|
| 81 |
public PrintStream outstream=System.err; |
| 82 |
public boolean addTab=true; |
| 83 |
} |