[fix] but primes behind an Arc

- don't clone primes for every thread, instead put it behind an Arc
  (atomic reference counted)
- print the pure calculation time at the end of the program
- don't show thread debug prints
This commit is contained in:
ddidderr 2024-09-28 12:57:29 +02:00
parent 939e239b36
commit 490c53a280
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -3,6 +3,7 @@ mod sieve;
use std::{
collections::HashMap,
env,
sync::Arc,
thread::{self, available_parallelism},
time::Instant,
};
@ -76,7 +77,6 @@ fn calculate_chunk_bounds(i: usize, num_threads: usize, max_nr: usize) -> (usize
let chunk_size = max_nr / num_threads;
let start = i * chunk_size;
let end = ((i + 1) * chunk_size).min(max_nr);
println!("Thread {i}: {start} - {end}");
(start, end)
}
@ -92,10 +92,13 @@ fn main() {
let primes = get_primes(MAX_SIEVED_PRIMES);
println!("{} primes. Took {:?}", primes.len(), start.elapsed());
let primes = Arc::new(primes);
#[allow(clippy::unwrap_used)]
let num_threads = available_parallelism().unwrap().get();
let mut threads = Vec::with_capacity(num_threads);
let now = Instant::now();
for i in 0..num_threads {
let (start, end) = calculate_chunk_bounds(i, num_threads, max_nr);
let primes = primes.clone();
@ -118,4 +121,6 @@ fn main() {
max_teilers = teilers;
}
}
println!("Took {:?}", now.elapsed());
}