From 490c53a2803296cf9cd36db61ec27ad404da4f19 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sat, 28 Sep 2024 12:57:29 +0200 Subject: [PATCH] [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 --- src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e37d2e1..475e116 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); }