Created
February 24, 2026 12:17
-
-
Save daviddanielng/5c9348a8122038a4ce5ff2b2fedb27a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::{ | |
| sync::{Arc, Mutex}, | |
| thread, | |
| time::Instant, | |
| }; | |
| enum CountType { | |
| Async, | |
| Sync, | |
| Thread { threads: u8 }, | |
| } | |
| fn main() { | |
| let count = 80_000_000_000; | |
| count_syn(count, CountType::Async); | |
| count_syn(count, CountType::Sync); | |
| count_syn(count, CountType::Thread { threads: 2 }); | |
| count_syn(count, CountType::Thread { threads: 5 }); | |
| count_syn(count, CountType::Thread { threads: 10 }); | |
| count_syn(count, CountType::Thread { threads: 20 }); | |
| } | |
| fn count_syn(count: u128, count_type: CountType) { | |
| let run_type; | |
| let start: Instant = Instant::now(); | |
| match count_type { | |
| CountType::Async => { | |
| run_type = "ASync".to_string(); | |
| println!("Not Implemented") | |
| } | |
| CountType::Sync => { | |
| run_type = "Sync".to_string(); | |
| let mut t_count: u128 = 1; | |
| for i in 0..(count + 1) { | |
| t_count = i; | |
| } | |
| println!("{t_count}") | |
| } | |
| CountType::Thread { threads } => { | |
| run_type = format!("Thread ({threads})"); | |
| let counter = Arc::new(Mutex::new(0)); | |
| let mut handles = vec![]; | |
| for _ in 0..threads { | |
| let counter = Arc::clone(&counter); | |
| let handle = thread::spawn(move || { | |
| let mut num = counter.lock().unwrap(); | |
| while *num < count { | |
| *num += 1; | |
| } | |
| }); | |
| handles.push(handle); | |
| } | |
| for handle in handles { | |
| handle.join().unwrap(); | |
| } | |
| println!("{}", *counter.lock().unwrap()); | |
| } | |
| } | |
| let secs = start.elapsed().as_secs_f64(); | |
| println!("{} Run Took {secs}s", run_type) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment