From fdd92dd5f739fde8f75d012657aa5ab943f2204f Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Tue, 31 Dec 2024 12:58:04 +0100 Subject: [PATCH] Make it more like libraries.rs, tried reading whole file into vec, but it's very slow, maybe slice is better --- .../src/implementations/multi_threaded.rs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/rust/src/implementations/multi_threaded.rs b/src/main/rust/src/implementations/multi_threaded.rs index 78ea9d8..589eee5 100644 --- a/src/main/rust/src/implementations/multi_threaded.rs +++ b/src/main/rust/src/implementations/multi_threaded.rs @@ -21,29 +21,28 @@ pub fn run() { let file_length = reader.seek(SeekFrom::End(0)).unwrap(); let chunk_length = file_length as usize / cores; let mut bounds = Vec::with_capacity(cores + 1); - bounds.push(0); - for i in 1..cores { + let mut start = 0; + for i in 0..cores { let mut reader = BufReader::new(&file); - let mut byte_start = chunk_length * i; + let mut end = chunk_length * i; reader - .seek(SeekFrom::Start(byte_start as u64)) + .seek(SeekFrom::Start(end as u64)) .expect("could not seek"); let mut line = Vec::with_capacity(108); let line_len = reader .read_until(b'\n', &mut line) .expect("could not read bytes"); - byte_start += line_len; - bounds.push(byte_start as u64); + end += line_len; + bounds.push((start, end)); + start = end + 1; } - bounds.push(file_length); for i in 0..cores { let tx = tx.clone(); - let mut currposition = *bounds.get(i).unwrap(); - let end = *bounds.get(i + 1).unwrap(); + let (mut currposition, end) = *bounds.get(i).unwrap(); s.spawn(move || { let file = File::open(FILE_PATH).expect("File measurements.txt not found"); let mut reader = BufReader::new(&file); - reader.seek(SeekFrom::Start(currposition)).unwrap(); + reader.seek(SeekFrom::Start(currposition as u64)).unwrap(); let mut t_stations: HashMap = HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); let mut line = Vec::with_capacity(108); @@ -70,7 +69,7 @@ pub fn run() { }; t_stations.insert(hash, (station.to_string(), measurements)); } - currposition += line_len as u64; + currposition += line_len; if currposition >= end { break; }