Resolves #102 and Code Optimizations

This commit is contained in:
Keshavram Kuduwa 2024-01-05 23:05:31 +05:30 committed by GitHub
parent c4879d4104
commit a53549ae50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,62 +18,124 @@ package dev.morling.onebrc;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.DoubleSummaryStatistics;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CalculateAverage_kuduwa_keshavram { public class CalculateAverage_kuduwa_keshavram {
private static final String FILE = "./measurements.txt"; private static final String FILE = "./measurements.txt";
private static final Function<String, String> KEY_MAPPER = line -> {
int pivot = line.indexOf(";");
return line.substring(0, pivot);
};
private static final ToDoubleFunction<String> VALUE_MAPPER = line -> {
int pivot = line.indexOf(";");
return toDouble(line.substring(pivot + 1));
};
private record Measurement(double min, double max, double sum, long count) { public static void main(String[] args) throws IOException, InterruptedException {
try (Stream<String> lines = Files.lines(Path.of(FILE))) {
Measurement(double initialMeasurement) { Map<String, DoubleSummaryStatistics> resultMap = lines
this(initialMeasurement, initialMeasurement, initialMeasurement, 1); .parallel()
.collect(
Collectors.groupingBy(KEY_MAPPER, Collectors.summarizingDouble(VALUE_MAPPER)));
System.out.println(
resultMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(
entry -> String.format(
"%s=%.1f/%.1f/%.1f",
entry.getKey(),
entry.getValue().getMin(),
entry.getValue().getAverage(),
entry.getValue().getMax()))
.collect(Collectors.joining(", ", "{", "}")));
}
} }
public static Measurement combineWith(Measurement m1, Measurement m2) { private static final long MAX_VALUE_DIVIDE_10 = Long.MAX_VALUE / 10;
return new Measurement(
m1.min < m2.min ? m1.min : m2.min, private static double toDouble(String num) {
m1.max > m2.max ? m1.max : m2.max, long value = 0;
m1.sum + m2.sum, int exp = 0;
m1.count + m2.count); boolean negative = false;
int decimalPlaces = Integer.MIN_VALUE;
for (byte ch : num.getBytes()) {
if (ch >= '0' && ch <= '9') {
value = value * 10 + (ch - '0');
decimalPlaces++;
}
else if (ch == '-') {
negative = true;
}
else if (ch == '.') {
decimalPlaces = 0;
}
else {
break;
}
}
return asDouble(value, exp, negative, decimalPlaces);
} }
public String toString() { private static double asDouble(long value, int exp, boolean negative, int decimalPlaces) {
return round(min) + "/" + round(sum / count) + "/" + round(max); if (decimalPlaces > 0 && value < Long.MAX_VALUE / 2) {
} if (value < Long.MAX_VALUE / (1L << 32)) {
exp -= 32;
private double round(double value) { value <<= 32;
return Math.round(value * 10.0) / 10.0; }
} if (value < Long.MAX_VALUE / (1L << 16)) {
} exp -= 16;
value <<= 16;
public static void main(String[] args) throws IOException { }
// long before = System.currentTimeMillis(); if (value < Long.MAX_VALUE / (1L << 8)) {
Map<String, Measurement> resultMap = new ConcurrentHashMap<>(); exp -= 8;
Files.lines(Path.of(FILE)) value <<= 8;
.parallel() }
.forEach( if (value < Long.MAX_VALUE / (1L << 4)) {
line -> { exp -= 4;
int pivot = line.indexOf(";"); value <<= 4;
String key = line.substring(0, pivot); }
Measurement measured = new Measurement(Double.parseDouble(line.substring(pivot + 1))); if (value < Long.MAX_VALUE / (1L << 2)) {
Measurement existingMeasurement = resultMap.get(key); exp -= 2;
if (existingMeasurement != null) { value <<= 2;
resultMap.put(key, Measurement.combineWith(existingMeasurement, measured)); }
} if (value < Long.MAX_VALUE / (1L << 1)) {
else { exp -= 1;
resultMap.put(key, measured); value <<= 1;
} }
}); }
System.out.print("{"); for (; decimalPlaces > 0; decimalPlaces--) {
System.out.print( exp--;
resultMap.entrySet().stream() long mod = value % 5;
.map(Object::toString) value /= 5;
.sorted() int modDiv = 1;
.collect(Collectors.joining(", "))); if (value < Long.MAX_VALUE / (1L << 4)) {
System.out.println("}"); exp -= 4;
// System.out.println("Took: " + (System.currentTimeMillis() - before)); value <<= 4;
modDiv <<= 4;
}
if (value < Long.MAX_VALUE / (1L << 2)) {
exp -= 2;
value <<= 2;
modDiv <<= 2;
}
if (value < Long.MAX_VALUE / (1L << 1)) {
exp -= 1;
value <<= 1;
modDiv <<= 1;
}
if (decimalPlaces > 1)
value += modDiv * mod / 5;
else
value += (modDiv * mod + 4) / 5;
}
final double d = Math.scalb((double) value, exp);
return negative ? -d : d;
} }
} }