| 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 |
package dr.util; |
| 27 |
|
| 28 |
public class Timer { |
| 29 |
|
| 30 |
private long start = 0, stop = 0; |
| 31 |
|
| 32 |
public void start() { |
| 33 |
start = System.currentTimeMillis(); |
| 34 |
} |
| 35 |
|
| 36 |
public void stop() { |
| 37 |
stop = System.currentTimeMillis(); |
| 38 |
} |
| 39 |
|
| 40 |
public void update() { |
| 41 |
stop = System.currentTimeMillis(); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public long calibrate(double fraction) { |
| 48 |
long timeTaken = System.currentTimeMillis() - start; |
| 49 |
|
| 50 |
return Math.round(((double)timeTaken / fraction) * (1.0 - fraction)); |
| 51 |
} |
| 52 |
|
| 53 |
public double toSeconds() { |
| 54 |
update(); |
| 55 |
return toSeconds(stop - start); |
| 56 |
} |
| 57 |
|
| 58 |
public static double toSeconds(long millis) { |
| 59 |
return millis / 1000.0; |
| 60 |
} |
| 61 |
|
| 62 |
public static double toMinutes(long millis) { |
| 63 |
return toSeconds(millis) / 60.0; |
| 64 |
} |
| 65 |
|
| 66 |
public static double toHours(long millis) { |
| 67 |
return toMinutes(millis) / 60.0; |
| 68 |
} |
| 69 |
|
| 70 |
public static double toDays(long millis) { |
| 71 |
return toHours(millis) / 24.0; |
| 72 |
} |
| 73 |
|
| 74 |
public String toString() { |
| 75 |
update(); |
| 76 |
return toString(stop - start); |
| 77 |
} |
| 78 |
|
| 79 |
public static String toString(long millis) { |
| 80 |
if (toDays(millis) > 1.0) return toDays(millis) + " days"; |
| 81 |
if (toHours(millis) > 1.0) return toHours(millis) + " hours"; |
| 82 |
if (toMinutes(millis) > 1.0) return toMinutes(millis) + " minutes"; |
| 83 |
return toSeconds(millis) + " seconds"; |
| 84 |
} |
| 85 |
} |