-
Notifications
You must be signed in to change notification settings - Fork 5
/
Stats.java
86 lines (75 loc) · 2.59 KB
/
Stats.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.genericsystem.cv.classifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public final class Stats {
private static Map<String, List<Long>> cumulativeTimers = new ConcurrentHashMap<>();
private static Map<String, Long> cumul = new ConcurrentHashMap<>();
private static Map<String, Long> timers = new ConcurrentHashMap<>();
private static Map<String, List<Long>> stats = new ConcurrentHashMap<>();
public static void beginCumulative(String key) {
cumul.put(key, System.nanoTime());
}
public static void endCumulative(String key) {
long value;
if (cumul.get(key) != null)
value = System.nanoTime() - cumul.get(key);
else
value = 0L;
List<Long> times = cumulativeTimers.get(key);
if (times == null)
times = new ArrayList<>();
times.add(value);
cumulativeTimers.put(key, times);
}
public static void resetCumulative(String key) {
List<Long> timers = cumulativeTimers.get(key);
long total = timers.stream().mapToLong(x -> x).sum();
cumulativeTimers.put(key, new ArrayList<>());
List<Long> times = stats.get(key);
if (times == null)
times = new ArrayList<>();
times.add(total);
stats.put(key, times);
}
public static void beginTask(String key) {
timers.put(key, System.nanoTime());
}
public static void endTask(String key) {
long value;
if (timers.get(key) != null)
value = System.nanoTime() - timers.get(key);
else
value = 0L;
List<Long> times = stats.get(key);
if (times == null)
times = new ArrayList<>();
times.add(value);
stats.put(key, times);
}
public static void reset() {
cumulativeTimers = new ConcurrentHashMap<>();
timers = new ConcurrentHashMap<>();
stats = new ConcurrentHashMap<>();
}
public static String getStatsAndReset() {
if (stats.isEmpty())
return "";
StringBuffer sb = new StringBuffer();
sb.append("\n").append("### STATS ###").append("\n");
stats.entrySet().forEach(entry -> {
double average = entry.getValue().stream().mapToDouble(l -> l.doubleValue()).average().getAsDouble();
double sem = Math.sqrt(entry.getValue().stream().mapToDouble(x -> Math.pow(x - average, 2)).sum() / (entry.getValue().size() - 1));
sb.append(entry.getKey()).append(": ");
sb.append(String.format("%.3f", average / 1_000_000));
sb.append(" ± ").append(String.format("%.3f", sem / 1_000_000));
sb.append("ms");
// sb.append(" | ").append(entry.getValue().stream().map(x -> x / 1_000_000).collect(Collectors.toList()));
sb.append("\n");
});
sb.append("### ----- ###").append("\n");
reset();
return sb.toString();
}
}