This commit is contained in:
Vinzent Steinberg 2017-05-05 13:44:02 +02:00
commit a1e432051e
2 changed files with 37 additions and 0 deletions

View File

@ -13,3 +13,4 @@ conv = "0.3"
[dev-dependencies]
rand = "0.3"
streaming-stats = "0.1"

View File

@ -1,8 +1,11 @@
#![no_std]
#![feature(test)]
extern crate conv;
#[cfg(test)] extern crate rand;
#[cfg(test)] #[macro_use] extern crate std;
#[cfg(test)] extern crate stats;
#[cfg(test)] extern crate test;
use conv::ApproxFrom;
@ -102,6 +105,8 @@ impl core::iter::FromIterator<f64> for Average {
mod tests {
use super::*;
use std::vec::Vec;
use ::conv::ConvAsUtil;
#[test]
@ -135,4 +140,35 @@ mod tests {
assert_almost_eq!(a.mean(), 2.0, 1e-2);
assert_almost_eq!(a.var().sqrt(), 3.0, 1e-2);
}
fn initialize_vec() -> Vec<f64> {
use rand::distributions::{Normal, IndependentSample};
use rand::{XorShiftRng, SeedableRng};
let normal = Normal::new(2.0, 3.0);
let n = 1_000_000;
let mut values = Vec::with_capacity(n);
let mut rng = XorShiftRng::from_seed([1, 2, 3, 4]);
for _ in 0..n {
values.push(normal.ind_sample(&mut rng));
}
values
}
#[bench]
fn bench_average(b: &mut test::Bencher) {
let values = initialize_vec();
b.iter(|| {
let a: Average = values.iter().map(|x| *x).collect();
a
});
}
#[bench]
fn bench_stats(b: &mut test::Bencher) {
let values = initialize_vec();
b.iter(|| {
let a: stats::OnlineStats = values.iter().map(|x| *x).collect();
a
});
}
}