rust-average/benches/kurtosis.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

2019-07-08 17:32:18 +02:00
#![cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp, map_clone))]
2019-07-08 16:08:53 +02:00
use bencher::{Bencher, benchmark_group, benchmark_main};
/// Create a random vector by sampling from a normal distribution.
fn initialize_vec() -> Vec<f64> {
2019-07-08 16:08:53 +02:00
use rand_distr::{Normal, Distribution};
2019-05-22 14:45:04 +02:00
use rand::SeedableRng;
2019-07-08 16:08:53 +02:00
let normal = Normal::new(2.0, 3.0).unwrap();
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]);
for _ in 0..n {
2018-07-06 10:56:35 +02:00
values.push(normal.sample(&mut rng));
}
values
}
fn bench_kurtosis(b: &mut Bencher) {
let values = initialize_vec();
b.iter(|| {
let m: average::Kurtosis = values.iter().map(|x| *x).collect();
m
});
}
fn bench_moments(b: &mut Bencher) {
let values = initialize_vec();
b.iter(|| {
2018-07-11 15:52:57 +02:00
let m: average::Moments4 = values.iter().map(|x| *x).collect();
m
});
}
benchmark_group!(benches, bench_kurtosis, bench_moments);
benchmark_main!(benches);