diff --git a/calculate_average_filiphr.sh b/calculate_average_filiphr.sh new file mode 100755 index 0000000..56d4e37 --- /dev/null +++ b/calculate_average_filiphr.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# +# 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. +# + + +sdk use java 21.0.1-graal +java -version +JAVA_OPTS="" +time java $JAVA_OPTS --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CalculateAverage_filiphr diff --git a/src/main/java/dev/morling/onebrc/CalculateAverage_filiphr.java b/src/main/java/dev/morling/onebrc/CalculateAverage_filiphr.java new file mode 100644 index 0000000..f75d99e --- /dev/null +++ b/src/main/java/dev/morling/onebrc/CalculateAverage_filiphr.java @@ -0,0 +1,235 @@ +/* + * 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.IOException; +import java.io.UncheckedIOException; +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.TreeMap; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +/** + * Initial submission: 1m 35s + * Adding memory mapped files: 0m 55s (based on bjhara's submission) + * Using big decimal and iterating the buffer once: 0m 20s + *
+ * Using 21.0.1 Temurin with ShenandoahGC on Macbook (Intel) Pro
+ * `sdk use java 21.0.1-tem`
+ *
+ * When using Oracle GraalVM 21.0.1+12.1
+ * `sdk use java 21.0.1-graal`
+ * It takes 0m 15s on my machine
+ * `sdk use java 21.0.1-graalce`
+ * It takes 0m 20s on my machine
+ *
+ * @author Filip Hrisafov
+ */
+public class CalculateAverage_filiphr {
+
+ private static final String FILE = "./measurements.txt";
+ private static final long CHUNK_SIZE = 1024 * 1024 * 10L; // 1KB * 10KB ~ 10MB
+
+ private static final class Measurement {
+
+ private double min = Long.MAX_VALUE;
+ private double max = Long.MIN_VALUE;
+ private double sum = 0L;
+ private long count = 0L;
+
+ private void add(double value) {
+ this.min = Math.min(this.min, value);
+ this.max = Math.max(this.max, value);
+ this.sum += value;
+ this.count++;
+ }
+
+ public static Measurement combine(Measurement m1, Measurement m2) {
+ Measurement measurement = new Measurement();
+ measurement.min = Math.min(m1.min, m2.min);
+ measurement.max = Math.max(m1.max, m2.max);
+ measurement.sum = m1.sum + m2.sum;
+ measurement.count = m1.count + m2.count;
+ return measurement;
+ }
+
+ @Override
+ public String toString() {
+ return round(min) + "/" + round((sum) / count) + "/" + round(max);
+ }
+
+ private double round(double value) {
+ return Math.round(value * 10.0) / 10.0;
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ // long start = System.nanoTime();
+
+ Map