Skip to content

Commit

Permalink
Add benchmark for integer log10.
Browse files Browse the repository at this point in the history
  • Loading branch information
Falk Hüffner committed Sep 6, 2021
1 parent b2d9bcd commit 0c26a3b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/core/benches/lib.rs
@@ -1,6 +1,7 @@
// wasm32 does not support benches (no time).
#![cfg(not(target_arch = "wasm32"))]
#![feature(flt2dec)]
#![feature(int_log)]
#![feature(test)]

extern crate test;
Expand Down
60 changes: 60 additions & 0 deletions library/core/benches/num/int_log/mod.rs
@@ -0,0 +1,60 @@
#![feature(int_log)]

use rand::Rng;
use test::{black_box, Bencher};

macro_rules! int_log_bench {
($t:ty, $predictable:ident, $random:ident, $random_small:ident) => {
#[bench]
fn $predictable(bench: &mut Bencher) {
bench.iter(|| {
for n in 0..(<$t>::BITS / 8) {
for i in 1..=(100 as $t) {
let x = black_box(i << (n * 8));
black_box(x.log10());
}
}
});
}

#[bench]
fn $random(bench: &mut Bencher) {
let mut rng = rand::thread_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 != 0 { x } else { 1 }
})
.collect();
bench.iter(|| {
for x in &numbers {
black_box(black_box(x).log10());
}
});
}

#[bench]
fn $random_small(bench: &mut Bencher) {
let mut rng = rand::thread_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 != 0 { x } else { 1 }
})
.collect();
bench.iter(|| {
for x in &numbers {
black_box(black_box(x).log10());
}
});
}
};
}

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}
1 change: 1 addition & 0 deletions library/core/benches/num/mod.rs
@@ -1,5 +1,6 @@
mod dec2flt;
mod flt2dec;
mod int_log;

use std::str::FromStr;
use test::Bencher;
Expand Down

0 comments on commit 0c26a3b

Please sign in to comment.