Debian Codegraph @

src/dr/util/Timer.java

raw symbols SCIP
1 /*
2 * Timer.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @return the estimated milliseconds left to complete a task.
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 }