Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- My found times:
- Debug (cargo run): 1.774342667s
- Release (cargo run --release): 14.744833ms
- Parallel (cargo run --release): 1.598625ms
- */
- use std::time::Instant;
- use rayon::prelude::*;
- fn flip_coins(num_heads: u32) -> bool {
- let mut heads_in_a_row = 0;
- while heads_in_a_row < num_heads {
- if rand::random_bool(0.5) {
- heads_in_a_row += 1;
- } else {
- return false;
- }
- }
- true
- }
- fn main() {
- let num_heads = 20;
- // Normal version
- let start_time = Instant::now();
- while !flip_coins(num_heads) {
- continue;
- }
- println!("Normal version: {:?}", start_time.elapsed());
- // Parallel version
- let start_time = Instant::now();
- (0u64..u64::MAX)
- .into_par_iter()
- .find_any(|_| flip_coins(num_heads));
- println!("Parallel version: {:?}", start_time.elapsed());
- }
Advertisement
Add Comment
Please, Sign In to add comment