rust-average/benches/min.rs

47 lines
1.2 KiB
Rust
Raw Normal View History

2017-05-30 10:54:14 +02:00
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
2017-05-24 18:58:34 +02:00
#[macro_use] extern crate bencher;
extern crate rand;
2019-05-22 14:45:04 +02:00
extern crate rand_xoshiro;
2017-05-24 18:58:34 +02:00
extern crate average;
extern crate stats;
use bencher::Bencher;
/// Create a random vector of random floats in [0, 1].
fn initialize_vec() -> Vec<f64> {
2019-05-22 14:45:04 +02:00
use rand::distributions::{Uniform, Distribution};
use rand::SeedableRng;
let range = Uniform::new(0.0, 1.0);
2017-05-24 18:58:34 +02:00
let n = 1_000_000;
let mut values = Vec::with_capacity(n);
2019-05-22 14:45:04 +02:00
let mut rng = rand_xoshiro::Xoshiro256StarStar::from_seed(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]);
2017-05-24 18:58:34 +02:00
for _ in 0..n {
2018-07-06 10:56:35 +02:00
values.push(range.sample(&mut rng));
2017-05-24 18:58:34 +02:00
}
values
}
fn bench_average(b: &mut Bencher) {
let values = initialize_vec();
b.iter(|| {
let a: average::Min = values.iter().map(|x| *x).collect();
a
});
}
fn bench_iter(b: &mut Bencher) {
let values = initialize_vec();
b.iter(|| {
let mut it = values.iter();
let init: f64 = *it.next().unwrap();
it.fold(init, |a, &b| a.min(b))
});
}
benchmark_group!(benches, bench_average, bench_iter);
benchmark_main!(benches);