Skip to content

Commit

Permalink
checked_ilog: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoStra committed Sep 18, 2023
1 parent 84a865c commit bc64e97
Showing 1 changed file with 73 additions and 6 deletions.
79 changes: 73 additions & 6 deletions library/core/benches/num/int_log/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::Rng;
use test::{black_box, Bencher};

macro_rules! int_log_bench {
macro_rules! int_log10_bench {
($t:ty, $predictable:ident, $random:ident, $random_small:ident) => {
#[bench]
fn $predictable(bench: &mut Bencher) {
Expand Down Expand Up @@ -51,8 +51,75 @@ macro_rules! int_log_bench {
};
}

int_log_bench! {u8, u8_log10_predictable, u8_log10_random, u8_log10_random_small}
int_log_bench! {u16, u16_log10_predictable, u16_log10_random, u16_log10_random_small}
int_log_bench! {u32, u32_log10_predictable, u32_log10_random, u32_log10_random_small}
int_log_bench! {u64, u64_log10_predictable, u64_log10_random, u64_log10_random_small}
int_log_bench! {u128, u128_log10_predictable, u128_log10_random, u128_log10_random_small}
int_log10_bench! {u8, u8_log10_predictable, u8_log10_random, u8_log10_random_small}
int_log10_bench! {u16, u16_log10_predictable, u16_log10_random, u16_log10_random_small}
int_log10_bench! {u32, u32_log10_predictable, u32_log10_random, u32_log10_random_small}
int_log10_bench! {u64, u64_log10_predictable, u64_log10_random, u64_log10_random_small}
int_log10_bench! {u128, u128_log10_predictable, u128_log10_random, u128_log10_random_small}

macro_rules! int_log_bench {
($t:ty, $random:ident, $random_small:ident, $geometric:ident) => {
#[bench]
fn $random(bench: &mut Bencher) {
let mut rng = crate::bench_rng();
/* Exponentially distributed random numbers from the whole range of the type. */
let numbers: Vec<$t> = (0..256)
.map(|_| {
let x = rng.gen::<$t>() >> rng.gen_range(0..<$t>::BITS);
if x >= 2 { x } else { 2 }
})
.collect();
bench.iter(|| {
for &b in &numbers {
for &x in &numbers {
black_box(black_box(x).ilog(b));
}
}
});
}

#[bench]
fn $random_small(bench: &mut Bencher) {
let mut rng = crate::bench_rng();
/* Exponentially distributed random numbers from the range 0..256. */
let numbers: Vec<$t> = (0..256)
.map(|_| {
let x = (rng.gen::<u8>() >> rng.gen_range(0..u8::BITS)) as $t;
if x >= 2 { x } else { 2 }
})
.collect();
bench.iter(|| {
for &b in &numbers {
for &x in &numbers {
black_box(black_box(x).ilog(b));
}
}
});
}

#[bench]
fn $geometric(bench: &mut Bencher) {
let bases: [$t; 16] = [2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65];
let base_and_numbers: Vec<($t, Vec<$t>)> = bases
.iter()
.map(|&b| {
let numbers = (0..=<$t>::MAX.ilog(b)).map(|exp| b.pow(exp)).collect();
(b, numbers)
})
.collect();
bench.iter(|| {
for (b, numbers) in &base_and_numbers {
for &x in numbers {
black_box(black_box(x).ilog(black_box(*b)));
}
}
});
}
};
}

int_log_bench! {u8, u8_log_random, u8_log_random_small, u8_log_geometric}
int_log_bench! {u16, u16_log_random, u16_log_random_small, u16_log_geometric}
int_log_bench! {u32, u32_log_random, u32_log_random_small, u32_log_geometric}
int_log_bench! {u64, u64_log_random, u64_log_random_small, u64_log_geometric}
int_log_bench! {u128, u128_log_random, u128_log_random_small, u128_log_geometric}

0 comments on commit bc64e97

Please sign in to comment.