Skip to content

Commit 6be6854

Browse files
committed
Added isqrt benchmark with uniform distribution.
1 parent 57b7217 commit 6be6854

File tree

1 file changed

+22
-14
lines changed
  • library/core/benches/num/int_sqrt

1 file changed

+22
-14
lines changed

library/core/benches/num/int_sqrt/mod.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rand::Rng;
22
use test::{black_box, Bencher};
33

44
macro_rules! int_sqrt_bench {
5-
($t:ty, $predictable:ident, $random:ident, $random_small:ident) => {
5+
($t:ty, $predictable:ident, $random:ident, $random_small:ident, $random_uniform:ident) => {
66
#[bench]
77
fn $predictable(bench: &mut Bencher) {
88
bench.iter(|| {
@@ -20,10 +20,7 @@ macro_rules! int_sqrt_bench {
2020
let mut rng = crate::bench_rng();
2121
/* Exponentially distributed random numbers from the whole range of the type. */
2222
let numbers: Vec<$t> = (0..256)
23-
.map(|_| {
24-
let x = rng.gen::<$t>() >> rng.gen_range(0..<$t>::BITS);
25-
if x != 0 { x } else { 1 }
26-
})
23+
.map(|_| rng.gen::<$t>() >> rng.gen_range(0..<$t>::BITS))
2724
.collect();
2825
bench.iter(|| {
2926
for x in &numbers {
@@ -37,10 +34,21 @@ macro_rules! int_sqrt_bench {
3734
let mut rng = crate::bench_rng();
3835
/* Exponentially distributed random numbers from the range 0..256. */
3936
let numbers: Vec<$t> = (0..256)
40-
.map(|_| {
41-
let x = (rng.gen::<u8>() >> rng.gen_range(0..u8::BITS)) as $t;
42-
if x != 0 { x } else { 1 }
43-
})
37+
.map(|_| (rng.gen::<u8>() >> rng.gen_range(0..u8::BITS)) as $t)
38+
.collect();
39+
bench.iter(|| {
40+
for x in &numbers {
41+
black_box(black_box(x).isqrt());
42+
}
43+
});
44+
}
45+
46+
#[bench]
47+
fn $random_uniform(bench: &mut Bencher) {
48+
let mut rng = crate::bench_rng();
49+
/* Exponentially distributed random numbers from the whole range of the type. */
50+
let numbers: Vec<$t> = (0..256)
51+
.map(|_| rng.gen::<$t>())
4452
.collect();
4553
bench.iter(|| {
4654
for x in &numbers {
@@ -51,8 +59,8 @@ macro_rules! int_sqrt_bench {
5159
};
5260
}
5361

54-
int_sqrt_bench! {u8, u8_sqrt_predictable, u8_sqrt_random, u8_sqrt_random_small}
55-
int_sqrt_bench! {u16, u16_sqrt_predictable, u16_sqrt_random, u16_sqrt_random_small}
56-
int_sqrt_bench! {u32, u32_sqrt_predictable, u32_sqrt_random, u32_sqrt_random_small}
57-
int_sqrt_bench! {u64, u64_sqrt_predictable, u64_sqrt_random, u64_sqrt_random_small}
58-
int_sqrt_bench! {u128, u128_sqrt_predictable, u128_sqrt_random, u128_sqrt_random_small}
62+
int_sqrt_bench! {u8, u8_sqrt_predictable, u8_sqrt_random, u8_sqrt_random_small, u8_sqrt_uniform}
63+
int_sqrt_bench! {u16, u16_sqrt_predictable, u16_sqrt_random, u16_sqrt_random_small, u16_sqrt_uniform}
64+
int_sqrt_bench! {u32, u32_sqrt_predictable, u32_sqrt_random, u32_sqrt_random_small, u32_sqrt_uniform}
65+
int_sqrt_bench! {u64, u64_sqrt_predictable, u64_sqrt_random, u64_sqrt_random_small, u64_sqrt_uniform}
66+
int_sqrt_bench! {u128, u128_sqrt_predictable, u128_sqrt_random, u128_sqrt_random_small, u128_sqrt_uniform}

0 commit comments

Comments
 (0)