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