From 53b3dcb456380c5632a33303c2f2e816d4249b97 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Mon, 11 Mar 2024 09:54:22 -0500 Subject: [PATCH] expands weighted-shuffle benchmarks Adding separate benchmarks for WeightedShuffle::new and WeightedShuffle::shuffle. --- gossip/benches/weighted_shuffle.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/gossip/benches/weighted_shuffle.rs b/gossip/benches/weighted_shuffle.rs index 58a7e37ce6a328..09615c57bbca15 100644 --- a/gossip/benches/weighted_shuffle.rs +++ b/gossip/benches/weighted_shuffle.rs @@ -11,18 +11,28 @@ use { }; fn make_weights(rng: &mut R) -> Vec { - 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::>() + let mut rng = ChaChaRng::from_seed(seed); + let shuffle = weighted_shuffle.clone().shuffle(&mut rng); + std::hint::black_box(shuffle.collect::>()); }); }