not working multi threaded solution
This commit is contained in:
parent
9606b06313
commit
14e55e8c5c
@ -2,55 +2,72 @@ use std::{
|
|||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufRead, BufReader},
|
io::{BufRead, BufReader},
|
||||||
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
|
thread,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StationMeasurements {
|
struct StationMeasurements {
|
||||||
min: f64,
|
min: f64,
|
||||||
max: f64,
|
max: f64,
|
||||||
temps: Vec<f64>,
|
count: usize,
|
||||||
|
sum: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut stations: HashMap<String, StationMeasurements> = HashMap::new();
|
let stations: Arc<Mutex<HashMap<String, StationMeasurements>>> =
|
||||||
|
Arc::new(Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
|
let cores: usize = std::thread::available_parallelism().unwrap().into();
|
||||||
|
|
||||||
let file = File::open("../measurements.txt").expect("File measurements.txt not found");
|
let file = File::open("../measurements.txt").expect("File measurements.txt not found");
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut line_num = 0;
|
let lines = Arc::new(Mutex::new(reader.lines()));
|
||||||
for line_result in reader.lines() {
|
let chunk_length = 1_000_000_000 / cores;
|
||||||
line_num += 1;
|
let mut handles = vec![];
|
||||||
if line_num % 50000 == 0 {
|
for _i in 0..cores {
|
||||||
println!("Calculated {line_num} stations");
|
let stations_clone = stations.clone();
|
||||||
}
|
let lines_clone = lines.clone();
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
|
let lines_guard = lines_clone.lock().unwrap();
|
||||||
|
let chunk = lines_guard.take(chunk_length);
|
||||||
|
lines_guard.skip(chunk_length);
|
||||||
|
for line_result in chunk {
|
||||||
let line = line_result.expect("could not read line");
|
let line = line_result.expect("could not read line");
|
||||||
let (station, temp) = line.split_once(';').unwrap();
|
let (station, temp) = line.split_once(';').unwrap();
|
||||||
let temp = temp.parse().unwrap();
|
let temp = temp.parse().unwrap();
|
||||||
let measurements_option = stations.get_mut(station);
|
let mut stations_guard = stations_clone.lock().unwrap();
|
||||||
|
let measurements_option = stations_guard.get_mut(station);
|
||||||
if let Some(measurements) = measurements_option {
|
if let Some(measurements) = measurements_option {
|
||||||
if temp < measurements.min {
|
if temp < measurements.min {
|
||||||
measurements.min = temp;
|
measurements.min = temp;
|
||||||
} else if temp > measurements.max {
|
} else if temp > measurements.max {
|
||||||
measurements.max = temp;
|
measurements.max = temp;
|
||||||
}
|
}
|
||||||
measurements.temps.push(temp);
|
measurements.count += 1;
|
||||||
|
measurements.sum += temp;
|
||||||
} else {
|
} else {
|
||||||
let measurements = StationMeasurements {
|
let measurements = StationMeasurements {
|
||||||
min: temp,
|
min: temp,
|
||||||
max: temp,
|
max: temp,
|
||||||
temps: vec![temp],
|
count: 1,
|
||||||
|
sum: temp,
|
||||||
};
|
};
|
||||||
stations.insert(station.to_owned(), measurements);
|
stations_clone
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(station.to_owned(), measurements);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (station, measurments) in stations {
|
});
|
||||||
|
handles.push(handle);
|
||||||
|
}
|
||||||
|
for handle in handles {
|
||||||
|
handle.join().unwrap();
|
||||||
|
}
|
||||||
|
for (station, measurments) in stations.lock().unwrap().iter() {
|
||||||
let min = measurments.min;
|
let min = measurments.min;
|
||||||
let max = measurments.max;
|
let max = measurments.max;
|
||||||
let avg = avg(measurments.temps);
|
let avg = measurments.sum / measurments.count as f64;
|
||||||
println!("{station}={min}/{max}/{avg}");
|
println!("{station}={min}/{max}/{avg:.2}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn avg(temps: Vec<f64>) -> f64 {
|
|
||||||
let num_temps = temps.len() as f64;
|
|
||||||
let sum_temps: f64 = temps.iter().sum();
|
|
||||||
sum_temps / num_temps
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user