Skip to content

Commit

Permalink
Add extra benchmark missing from rust-random#1530
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Nov 26, 2024
1 parent 50e5f95 commit 7813eb0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions benches/benches/seq_choose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ pub fn bench(c: &mut Criterion) {
});
}

let lens = [(1, 1000), (950, 1000), (10, 100), (90, 100)];
for (amount, len) in lens {
let name = format!("seq_slice_choose_multiple_weighted_{}_of_{}", amount, len);
c.bench_function(name.as_str(), |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
let mut buf = [0i32; 1000];
rng.fill(&mut buf);
let x = black_box(&buf[..len]);

let mut results_buf = [0i32; 950];
let y = black_box(&mut results_buf[..amount]);
let amount = black_box(amount);

b.iter(|| {
// Collect full result to prevent unwanted shortcuts getting
// first element (in case sample_indices returns an iterator).
for (slot, sample) in y.iter_mut().zip(x.choose_multiple_weighted(&mut rng, amount, |_| 1.0).unwrap()) {
*slot = *sample;
}
y[amount - 1]
})
});
}

c.bench_function("seq_iter_choose_multiple_10_of_100", |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
let mut buf = [0i32; 100];
Expand Down

0 comments on commit 7813eb0

Please sign in to comment.