Skip to content

Commit cce8a7a

Browse files
committed
benchmarks: restore benchmarks
In #128 we dropped the benchmarks. They can be restore with only minor tweaks, so do so.
1 parent 72421dc commit cce8a7a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/lib.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,81 @@ mod tests {
705705
assert!(decode(s).is_ok());
706706
}
707707
}
708+
#[cfg(bench)]
709+
mod benches {
710+
use test::{black_box, Bencher};
711+
712+
use crate::{Bech32, Bech32m};
713+
714+
#[bench]
715+
fn bech32_parse_address(bh: &mut Bencher) {
716+
let addr = black_box("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq");
717+
718+
bh.iter(|| {
719+
let tuple = crate::decode(&addr).expect("address is well formed");
720+
black_box(&tuple);
721+
})
722+
}
723+
724+
#[bench]
725+
fn bech32m_parse_address(bh: &mut Bencher) {
726+
let addr = black_box("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297");
727+
728+
bh.iter(|| {
729+
let tuple = crate::decode(&addr).expect("address is well formed");
730+
black_box(&tuple);
731+
})
732+
}
733+
734+
// Encode with allocation.
735+
#[bench]
736+
fn encode_bech32_address(bh: &mut Bencher) {
737+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
738+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
739+
740+
bh.iter(|| {
741+
let s = crate::encode::<Bech32>(hrp, &data).expect("failed to encode");
742+
black_box(&s);
743+
});
744+
}
745+
746+
// Encode without allocation.
747+
#[bench]
748+
fn encode_to_fmt_bech32_address(bh: &mut Bencher) {
749+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
750+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
751+
let mut buf = String::with_capacity(64);
752+
753+
bh.iter(|| {
754+
let res =
755+
crate::encode_to_fmt::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
756+
black_box(&res);
757+
});
758+
}
759+
760+
// Encode with allocation.
761+
#[bench]
762+
fn encode_bech32m_address(bh: &mut Bencher) {
763+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
764+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
765+
766+
bh.iter(|| {
767+
let s = crate::encode::<Bech32m>(hrp, &data).expect("failed to encode");
768+
black_box(&s);
769+
});
770+
}
771+
772+
// Encode without allocation.
773+
#[bench]
774+
fn encode_to_fmt_bech32m_address(bh: &mut Bencher) {
775+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
776+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
777+
let mut buf = String::with_capacity(64);
778+
779+
bh.iter(|| {
780+
let res =
781+
crate::encode_to_fmt::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
782+
black_box(&res);
783+
});
784+
}
785+
}

0 commit comments

Comments
 (0)