[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:
parent
939e239b36
commit
490c53a280
@ -3,6 +3,7 @@ mod sieve;
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
env,
|
env,
|
||||||
|
sync::Arc,
|
||||||
thread::{self, available_parallelism},
|
thread::{self, available_parallelism},
|
||||||
time::Instant,
|
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 chunk_size = max_nr / num_threads;
|
||||||
let start = i * chunk_size;
|
let start = i * chunk_size;
|
||||||
let end = ((i + 1) * chunk_size).min(max_nr);
|
let end = ((i + 1) * chunk_size).min(max_nr);
|
||||||
println!("Thread {i}: {start} - {end}");
|
|
||||||
(start, end)
|
(start, end)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,10 +92,13 @@ fn main() {
|
|||||||
let primes = get_primes(MAX_SIEVED_PRIMES);
|
let primes = get_primes(MAX_SIEVED_PRIMES);
|
||||||
println!("{} primes. Took {:?}", primes.len(), start.elapsed());
|
println!("{} primes. Took {:?}", primes.len(), start.elapsed());
|
||||||
|
|
||||||
|
let primes = Arc::new(primes);
|
||||||
|
|
||||||
#[allow(clippy::unwrap_used)]
|
#[allow(clippy::unwrap_used)]
|
||||||
let num_threads = available_parallelism().unwrap().get();
|
let num_threads = available_parallelism().unwrap().get();
|
||||||
let mut threads = Vec::with_capacity(num_threads);
|
let mut threads = Vec::with_capacity(num_threads);
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
for i in 0..num_threads {
|
for i in 0..num_threads {
|
||||||
let (start, end) = calculate_chunk_bounds(i, num_threads, max_nr);
|
let (start, end) = calculate_chunk_bounds(i, num_threads, max_nr);
|
||||||
let primes = primes.clone();
|
let primes = primes.clone();
|
||||||
@ -118,4 +121,6 @@ fn main() {
|
|||||||
max_teilers = teilers;
|
max_teilers = teilers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("Took {:?}", now.elapsed());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user