/* * 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.nio.file.Files; import java.nio.file.Paths; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; /** * This is the solution from GitHut Copilot Chat with the help of Antonio Goncalves (prompting and guiding, but trying not to change code directly on my own, always using Copilot). *
* List of prompts that has been used: *
* =============
* =============
* =============
* v1 - 73603 ms
* You are entering The One Billion Row Challenge (1BRC) which is an exploration of how far modern Java can be pushed for aggregating one billion rows from a text file. Grab all the (virtual) threads, reach out to SIMD, optimize the GC, or pull any other trick, and create the fastest implementation for solving this task!
* The text file contains temperature values for a range of weather stations. Each row is one measurement in the format
* The profiler mentions that this line of code has very bad performance. Can you refactor it so it has better performance:
* ---
* String[] parts = line.split(";")
* ---
*
* There is a maximum of 10000 unique station names. Can you optimize the code taking this into account?
* =============
* =============
* =============
* v4 - 56417 ms
* Which parameters can I pass to the JVM to make it run faster ?
* Which GC can I use and what is the most optimized to run CalculateAverage ?
*/
public class CalculateAverage_agoncal {
private static final String FILE = "./measurements.txt";
record Measurement(String station, double temperature) {
}
static class StationStats {
double min;
double max;
double sum;
int count;
public StationStats(double temperature) {
this.min = temperature;
this.max = temperature;
this.sum = 0;
this.count = 0;
}
synchronized void update(double temperature) {
min = Math.min(min, temperature);
max = Math.max(max, temperature);
sum += temperature;
count++;
}
double getAverage() {
return round(sum) / count;
}
@Override
public String toString() {
return String.format("%.1f/%.1f/%.1f", round(min), round(getAverage()), round(max));
}
}
public static void main(String[] args) throws IOException {
Map