@@ -2,7 +2,7 @@ use rand::Rng;
2
2
use test:: { black_box, Bencher } ;
3
3
4
4
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 ) => {
6
6
#[ bench]
7
7
fn $predictable( bench: & mut Bencher ) {
8
8
bench. iter( || {
@@ -20,10 +20,7 @@ macro_rules! int_sqrt_bench {
20
20
let mut rng = crate :: bench_rng( ) ;
21
21
/* Exponentially distributed random numbers from the whole range of the type. */
22
22
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 ) )
27
24
. collect( ) ;
28
25
bench. iter( || {
29
26
for x in & numbers {
@@ -37,10 +34,21 @@ macro_rules! int_sqrt_bench {
37
34
let mut rng = crate :: bench_rng( ) ;
38
35
/* Exponentially distributed random numbers from the range 0..256. */
39
36
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>( ) )
44
52
. collect( ) ;
45
53
bench. iter( || {
46
54
for x in & numbers {
@@ -51,8 +59,8 @@ macro_rules! int_sqrt_bench {
51
59
} ;
52
60
}
53
61
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