#![feature(test)] extern crate test; use std::mem::size_of; use test::Bencher; const RAND_BENCH_N: u64 = 1000; #[bench] fn shuffle_100(b: &mut Bencher) { let mut rng = urandom::new(); let mut x = [1usize; 100]; b.iter(|| { rng.shuffle(&mut x); x[0] }) } #[bench] fn choose_1_of_1000(b: &mut Bencher) { let mut rng = urandom::new(); let mut x = [1usize; 1000]; for i in 0..1000 { x[i] = i; } b.iter(|| { let mut s = 0; for _ in 0..RAND_BENCH_N { s += rng.choose(&x).unwrap(); } s }); b.bytes = size_of::() as u64 * crate::RAND_BENCH_N; } #[bench] fn single_from_1000(b: &mut Bencher) { let mut rng = urandom::new(); let mut x = [1usize; 1000]; for i in 0..1000 { x[i] = i; } b.iter(|| { let mut s = 0; for _ in 0..RAND_BENCH_N { s += rng.single(&x).unwrap(); } s }); b.bytes = size_of::() as u64 * crate::RAND_BENCH_N; } macro_rules! multiple { ($name:ident, $amount:expr, $length:expr) => { #[bench] fn $name(b: &mut Bencher) { let mut rng = urandom::new(); let x = [$amount; $length]; let mut result = [0; $amount]; b.iter(|| { rng.multiple(x.iter().cloned(), &mut result); result[$amount - 1] }) } }; } multiple!(multiple_1_of_1000, 1, 1000); multiple!(multiple_950_of_1000, 950, 1000); multiple!(multiple_10_of_100, 10, 100); multiple!(multiple_90_of_100, 90, 100);