use easy_parallel from smol project instead of std::thread. No performance improvement, but easier

This commit is contained in:
Fabian Schmidt 2024-08-27 13:23:51 +02:00
parent e832475fc3
commit a45ddd2dc0

View File

@ -1,23 +1,21 @@
use std::collections::HashMap;
use smol::fs::File;
use smol::io::{AsyncBufReadExt, AsyncSeekExt, BufReader, SeekFrom};
use std::collections::HashMap;
use crate::models::station_measurements::StationMeasurements;
use crate::utils::parse;
use std::sync::mpsc;
use crate::utils::parse::hashstr;
use easy_parallel::Parallel;
use std::thread;
use std::time::Instant;
use crate::utils::parse::hashstr;
const DEFAULT_HASHMAP_LENGTH: usize = 10000;
pub fn run() {
const FILE_PATH: &str = "../../../measurements.txt";
let now = Instant::now();
thread::scope(|s| {
let mut stations: HashMap<usize, (String, StationMeasurements)> =
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
let (tx, rx) = mpsc::channel();
let cores = thread::available_parallelism().unwrap().into();
let bounds = smol::block_on(async {
let mut file = File::open(FILE_PATH)
@ -46,11 +44,10 @@ pub fn run() {
bounds.push(file_length);
bounds
});
for i in 0..cores {
let tx = tx.clone();
let t_stations_vec = Parallel::new()
.each(0..cores, |i| {
let mut currposition = *bounds.get(i).unwrap();
let end = *bounds.get(i + 1).unwrap();
s.spawn(move || {
smol::block_on(async {
let mut file = File::open(FILE_PATH)
.await
@ -90,12 +87,11 @@ pub fn run() {
}
line.clear();
}
let _ = tx.send(t_stations);
t_stations
})
});
}
drop(tx);
while let Ok(t_stations) = rx.recv() {
})
.run();
for t_stations in t_stations_vec {
for (hash, (station, measurements)) in t_stations.iter() {
let joined_measurements_options = stations.get_mut(hash);
if let Some((_, joined_measurements)) = joined_measurements_options {
@ -130,5 +126,4 @@ pub fn run() {
println!("\n\n{{{stations}}}");
}
println!("\n\nTime={} ms", now.elapsed().as_millis());
});
}