Merge branch 'master' of https://github.com/vks/average
This commit is contained in:
commit
a1e432051e
@ -13,3 +13,4 @@ conv = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "0.3"
|
||||
streaming-stats = "0.1"
|
||||
|
36
src/lib.rs
36
src/lib.rs
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user