From ec5c6008f2dfda0814bb774344638b2452421296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Tue, 28 May 2024 03:14:27 +0300 Subject: [PATCH] feat: :sparkles: interaction fix: :bug: checks --- 20-multithread_prime_numbers/src/main.rs | 129 ++++++++++++----------- 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/20-multithread_prime_numbers/src/main.rs b/20-multithread_prime_numbers/src/main.rs index 2f2bf2b..105a838 100644 --- a/20-multithread_prime_numbers/src/main.rs +++ b/20-multithread_prime_numbers/src/main.rs @@ -16,83 +16,83 @@ fn get_input() -> u32 { }; } } -fn prime(start: u32, finish: u32) { - // if start > finish { - // println!("None"); - // return; - // } +fn prime(mut start: u32, finish: u32) { + if start > finish { + println!("None"); + return; + } - // if finish < 2 { - // println!("None"); - // return; - // } + if finish < 2 { + println!("None"); + return; + } // println!("\t2"); - // if finish == 2 { - // return; - // } + if finish == 2 { + return; + } - // if start < 3 { - // start = 3; - // } + if start < 3 { + start = 3; + } - // if start % 2 == 0 { - // start += 1; - // } - //let mut b; + if start % 2 == 0 { + start += 1; + } + // let mut b; for i in (start..=finish).step_by(2) { - //b = true; + // b = true; let square_root = (i as f64).sqrt() as u32; for j in (3..=square_root).step_by(2) { if i % j == 0 { - //println!("{} is even", i); - //b = false; + // println!("{} is even", i); + // b = false; break; } } - // if b == true { + // if b { // println!("\t{}", i); // } } } -async fn prime_async(start: u32, finish: u32) { - // if start > finish { - // println!("None"); - // return; - // } +async fn prime_async(mut start: u32, finish: u32) { + if start > finish { + println!("None"); + return; + } - // if finish < 2 { - // println!("None"); - // return; - // } + if finish < 2 { + println!("None"); + return; + } // println!("\t2"); - // if finish == 2 { - // return; - // } + if finish == 2 { + return; + } - // if start < 3 { - // start = 3; - // } + if start < 3 { + start = 3; + } - // if start % 2 == 0 { - // start += 1; - // } - //let mut b; + if start % 2 == 0 { + start += 1; + } + // let mut b; for i in (start..=finish).step_by(2) { - //b = true; + // b = true; let square_root = (i as f64).sqrt() as u32; for j in (3..=square_root).step_by(2) { if i % j == 0 { - //println!("{} is even", i); - //b = false; + // println!("{} is even", i); + // b = false; break; } } - // if b == true { + // if b { // println!("\t{}", i); // } } @@ -101,20 +101,18 @@ async fn prime_async(start: u32, finish: u32) { async fn main() { println!("Hello, world!"); - // println!("Start Point for Prime = ↓"); - // let start = get_input(); + println!("Start Point for Prime = ↓"); + let start = get_input(); - // println!("End Point for Prime = ↓"); - // let finish = get_input(); + println!("End Point for Prime = ↓"); + let finish = get_input(); - println!("How Many Thread Do You Want = ↓"); + println!("How Many Thread Do You Want at Max = ↓"); let thread_count = get_input(); - let start = 3; - let finish = 10000000; - let mut time_values_normal_thread = vec![]; let mut time_values_tokio_thread = vec![]; + let normal_thread_total_instant = Instant::now(); for i in 1..thread_count + 1 { let thread_count = i; let mut threads = vec![]; @@ -131,7 +129,9 @@ async fn main() { time_values_normal_thread.push(time_elapsed); println!("Elapsed: {:#?} with {} Normal Thread(s)", time_elapsed, i); } + let normal_thread_total_elapsed = normal_thread_total_instant.elapsed(); + let tokio_thread_total_instant = Instant::now(); for i in 1..thread_count + 1 { let thread_count = i; let mut threads = vec![]; @@ -148,30 +148,31 @@ async fn main() { time_values_tokio_thread.push(time_elapsed); println!("Elapsed: {:#?} with {} Tokio Thread(s)", time_elapsed, i); } + let tokio_thread_total_elapsed = tokio_thread_total_instant.elapsed(); let mut normal_bench = (Duration::MAX, 0); - for i in 0..time_values_normal_thread.len() { - if normal_bench.0 > time_values_normal_thread[i] { - normal_bench.0 = time_values_normal_thread[i]; + for (i, thread_time_elapsed) in time_values_normal_thread.iter().enumerate() { + if normal_bench.0 > *thread_time_elapsed { + normal_bench.0 = *thread_time_elapsed; normal_bench.1 = i + 1; } } let mut tokio_bench = (Duration::MAX, 0); - for i in 0..time_values_tokio_thread.len() { - if tokio_bench.0 > time_values_tokio_thread[i] { - tokio_bench.0 = time_values_tokio_thread[i]; + for (i, thread_time_elapsed) in time_values_tokio_thread.iter().enumerate() { + if tokio_bench.0 > *thread_time_elapsed { + tokio_bench.0 = *thread_time_elapsed; tokio_bench.1 = i + 1; } } println!( - "\n\nNormal Thread | Bench Results: Min time {:#?} with {} thread", - normal_bench.0, normal_bench.1 + "\n\nNormal Thread | Bench Results: Min time {:#?} with {} thread\n\tTotal Time Elapsed: {:#?}", + normal_bench.0, normal_bench.1, normal_thread_total_elapsed, ); println!( - "\n\nTokio Thread | Bench Results: Min time {:#?} with {} thread", - tokio_bench.0, tokio_bench.1 + "\n\nTokio Thread | Bench Results: Min time {:#?} with {} thread\n\tTotal Time Elapsed: {:#?}", + tokio_bench.0, tokio_bench.1, tokio_thread_total_elapsed, ); }