Skip to content

Instantly share code, notes, and snippets.

@heyajulia
Created May 22, 2023 10:38
Show Gist options
  • Save heyajulia/c2449b90d2c10bc3b52c798619e78811 to your computer and use it in GitHub Desktop.
Save heyajulia/c2449b90d2c10bc3b52c798619e78811 to your computer and use it in GitHub Desktop.
use rand::distributions::Uniform;
use rand::Rng;
use rayon::prelude::*;
use std::time::Instant;
fn main() {
let dis = Uniform::from(0..=6_u8);
for trials in (1..).map(|x| 10usize.pow(x)) {
let start = Instant::now();
let result = (0..trials)
.into_par_iter()
.map(|_| {
let mut rng = rand::thread_rng();
let balls = (0..20).map(|_| rng.sample(dis));
let mut bitset: u8 = 0;
for ball in balls {
bitset |= 1 << ball;
}
bitset.count_ones() as f64
})
.sum::<f64>();
let end = Instant::now();
println!(
"trials: {trials} result: {:.9} time: {:?}",
result / trials as f64,
end - start
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment