aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_ddimtirov.java
blob: 285ab00f0e89608c44618ce87d7977aef13da1ad (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 *  Copyright 2023 The original authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package dev.morling.onebrc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.TreeMap;
import java.util.stream.Collector;

import static java.util.stream.Collectors.groupingByConcurrent;

// gunnar morling - 2:10
// roy van rijn -   1:01
//                  0:53

public class CalculateAverage_ddimtirov {
    public record InputLine(String station, int value) {

        public static InputLine fromLine(String line) {
            int endOfText = line.indexOf(";");

            String station = line.substring(0, endOfText);

            int startOfWhole = endOfText + 1;
            int sign;
            if (line.charAt(startOfWhole) == '-') {
                sign = -1;
                startOfWhole++;
            } else {
                sign = 1;
            }

            int endOfWhole = line.lastIndexOf(".");
            var whole = unsafeParsePositiveInt(line, startOfWhole, endOfWhole);
            var decimal = unsafeParsePositiveInt(line,endOfWhole+1, line.length());
            int fixpoint10 = (whole * 10  + decimal) * sign;

            return new InputLine(station, fixpoint10);
        }

        static int unsafeParsePositiveInt(String s, int start, int end) {
            int acc = 0;
            for (int i = start; i<end; i++) {
                if (acc != 0) acc *= 10;
                char c = s.charAt(i);
                var v = c - '0';
                assert v>=0 && v<=9 : String.format("Character '%s', value %,d", c, v);
                acc += v;
            }
            return acc;
        }
    }

    private static class OutputMetrics {
        private int min = Integer.MAX_VALUE;
        private int max = Integer.MIN_VALUE;
        private long sum;
        private long count;

        @SuppressWarnings("ManualMinMaxCalculation")
        public OutputMetrics combine(OutputMetrics o) {
            var r = new OutputMetrics();
            r.min = min < o.min ? min : o.min;
            r.max = max > o.max ? max : o.max;
            r.sum = sum + o.sum;
            r.count = count + o.count;
            return r;
        }

        public void accumulate(InputLine m) {
            if (m.value < min) min = m.value;
            if (m.value > max) max = m.value;
            sum += m.value;
            count++;
        }

        @Override
        public String toString() {
            var min = this.min / 10.0;
            var mean = Math.round(this.sum / (double) count) / 10.0;
            var max = this.max / 10.0;
            return min + "/" + mean + "/" + max;
        }
    }

    public static void main(String[] args) throws IOException {
//         var start = Instant.now();
        Instant start = null;
        var path = Path.of("./measurements.txt");
        var buffer = 8192 * 20;

        // Files.lines() is optimized for files that can be indexed by int
        // For larger files it falls back to buffered reader, which we now
        // use directly to be able to tweak the buffer size.
        try (var reader = new BufferedReader(new InputStreamReader(Files.newInputStream(path)), buffer)) {
            var stationsToMetrics = reader.lines()
                    .map(InputLine::fromLine)
                    .parallel()
                    .collect(groupingByConcurrent(InputLine::station, Collector.of(
                            OutputMetrics::new,
                            OutputMetrics::accumulate,
                            OutputMetrics::combine,
                            OutputMetrics::toString
                    )));
            System.out.println(new TreeMap<>(stationsToMetrics));
            assert Files.readAllLines(Path.of("expected_result.txt")).getFirst().equals(new TreeMap<>(stationsToMetrics).toString());
        }

        //noinspection ConstantValue
        if (start!=null) System.err.println(Duration.between(start, Instant.now()));
    }

}