diff options
| author | Sam Pullara <spullara@gmail.com> | 2024-01-03 06:35:51 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-03 15:35:51 +0100 |
| commit | c832d64afe6b2039fdb5d42059d9b6cd9bce75df (patch) | |
| tree | 5aec67da1222c7c19a32d5a109ed21ca038ea336 /src/main/java/dev/morling/onebrc/CalculateAverage_naive.java | |
| parent | 0fc9e8d545cea77bb1dca1c6f32955a5f58d0d26 (diff) | |
Adding Sam Pullara's submission;
Diffstat (limited to 'src/main/java/dev/morling/onebrc/CalculateAverage_naive.java')
| -rw-r--r-- | src/main/java/dev/morling/onebrc/CalculateAverage_naive.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/java/dev/morling/onebrc/CalculateAverage_naive.java b/src/main/java/dev/morling/onebrc/CalculateAverage_naive.java new file mode 100644 index 0000000..54025d9 --- /dev/null +++ b/src/main/java/dev/morling/onebrc/CalculateAverage_naive.java @@ -0,0 +1,50 @@ +/* + * 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.FileNotFoundException; +import java.io.FileReader; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.stream.Collectors; + +public class CalculateAverage_naive { + + record Result(double min, double max, double sum, long count) { + } + + public static void main(String[] args) throws FileNotFoundException { + long start = System.currentTimeMillis(); + var results = new BufferedReader(new FileReader("./measurements.txt")) + .lines() + .map(l -> l.split(";")) + .collect(Collectors.toMap( + parts -> parts[0], + parts -> { + double temperature = Double.parseDouble(parts[1]); + return new Result(temperature, temperature, temperature, 1); + }, + (oldResult, newResult) -> { + double min = Math.min(oldResult.min, newResult.min); + double max = Math.max(oldResult.max, newResult.max); + double sum = oldResult.sum + newResult.sum; + long count = oldResult.count + newResult.count; + return new Result(min, max, sum, count); + }, ConcurrentSkipListMap::new)); + System.out.println(System.currentTimeMillis() - start); + System.out.println(results); + } +} |
