Skip to content

Commit

Permalink
expands weighted-shuffle benchmarks (anza-xyz#179)
Browse files Browse the repository at this point in the history
Adding separate benchmarks for WeightedShuffle::new and
WeightedShuffle::shuffle.
  • Loading branch information
behzadnouri authored and willhickey committed Mar 16, 2024
1 parent 365922d commit 4d3546a
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 4d3546a

Please sign in to comment.