forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStopwatch.java
More file actions
61 lines (52 loc) · 1.82 KB
/
Stopwatch.java
File metadata and controls
61 lines (52 loc) · 1.82 KB
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
// Copyright (c) 2014-2019 K Team. All Rights Reserved.
package org.kframework.utils;
import org.kframework.main.GlobalOptions;
import org.kframework.utils.inject.RequestScoped;
import com.google.inject.Inject;
import java.util.Formatter;
/**
* To use, access {@link #instance()} after calling {@link #init(GlobalOptions) init()}.
*/
@RequestScoped
public class Stopwatch {
private long start;
private long lastIntermediate;
Formatter f = new Formatter(System.out);
private final GlobalOptions options;
@Inject
public Stopwatch(GlobalOptions options) {
this.options = options;
start = System.currentTimeMillis();
lastIntermediate = start;
}
public void start() {
printIntermediate("Init");
}
public void printIntermediate(String message) {
long current = System.currentTimeMillis();
if (options.verbose)
f.format("%-60s = %s%n", message, milisecondsToTime(current - lastIntermediate));
lastIntermediate = current;
}
public void printTotal(String message) {
printIntermediate("Cleanup");
if (options.verbose)
f.format("%-60s = %s%n", message, milisecondsToTime(lastIntermediate - start));
}
private static String milisecondsToTime(long miliseconds) {
long h = miliseconds / 3600000;
long m = miliseconds % 3600000 / 60000;
double s = miliseconds % 60000 / 1000.;
if (h > 0)
return String.format("%dh %02dm %02ds", h, m, (long) s);
if (m > 0)
return String.format("%02dm %02ds", m, (long) s);
return String.format("%6.3fs", s);
}
public long getIntermediateMilliseconds() {
long endd = System.currentTimeMillis();
long rez = lastIntermediate - endd;
lastIntermediate = endd;
return rez;
}
}