Skip to content

Commit

Permalink
expands weighted-shuffle benchmarks
Browse files Browse the repository at this point in the history
Adding separate benchmarks for WeightedShuffle::new and
WeightedShuffle::shuffle.
  • Loading branch information
behzadnouri committed Mar 11, 2024
1 parent 3863bb1 commit 53b3dcb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions gossip/benches/weighted_shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ use {
};

fn make_weights<R: Rng>(rng: &mut R) -> Vec<u64> {
repeat_with(|| rng.gen_range(1..100)).take(1000).collect()
repeat_with(|| rng.gen_range(1..100)).take(4000).collect()
}

#[bench]
fn bench_weighted_shuffle(bencher: &mut Bencher) {
fn bench_weighted_shuffle_new(bencher: &mut Bencher) {
let mut rng = rand::thread_rng();
bencher.iter(|| {
let weights = make_weights(&mut rng);
std::hint::black_box(WeightedShuffle::new("", &weights));
});
}

#[bench]
fn bench_weighted_shuffle_shuffle(bencher: &mut Bencher) {
let mut seed = [0u8; 32];
let mut rng = rand::thread_rng();
let weights = make_weights(&mut rng);
let weighted_shuffle = WeightedShuffle::new("", &weights);
bencher.iter(|| {
rng.fill(&mut seed[..]);
WeightedShuffle::new("", &weights)
.shuffle(&mut ChaChaRng::from_seed(seed))
.collect::<Vec<_>>()
let mut rng = ChaChaRng::from_seed(seed);
let shuffle = weighted_shuffle.clone().shuffle(&mut rng);
std::hint::black_box(shuffle.collect::<Vec<_>>());
});
}

0 comments on commit 53b3dcb

Please sign in to comment.