From 665ac263335f9d911113e9472058d505fde605e9 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Fri, 5 May 2017 13:22:45 +0200 Subject: [PATCH] Add some benchmarks --- Cargo.toml | 1 + src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index e562795..e031ab2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ conv = "0.3" [dev-dependencies] rand = "0.3" +streaming-stats = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 9314db9..5d00ada 100644 --- a/src/lib.rs +++ b/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; @@ -90,6 +93,8 @@ impl core::iter::FromIterator for Average { mod tests { use super::*; + use std::vec::Vec; + use ::conv::ConvAsUtil; #[test] @@ -123,4 +128,35 @@ mod tests { assert_almost_eq!(a.avg(), 2.0, 1e-2); assert_almost_eq!(a.var().sqrt(), 3.0, 1e-2); } + + fn initialize_vec() -> Vec { + 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 + }); + } }