From 9bfae4419a7bfbb561eb5e1e951040cf53c3519d Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Tue, 26 Feb 2019 16:06:32 -0500 Subject: [PATCH] Add some benchmarks The primary purpose of this commit is to set up the benchmarking infrastructure and add some examples so that it's easier to add more benchmarks in the future. --- Cargo.toml | 5 ++ benches/quantile.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 benches/quantile.rs diff --git a/Cargo.toml b/Cargo.toml index 57795ce8..7352c78f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,11 @@ rand = "0.6" itertools = { version = "0.7.0", default-features = false } [dev-dependencies] +criterion = "0.2" quickcheck = "0.7" ndarray-rand = "0.9" approx = "0.3" + +[[bench]] +name = "quantile" +harness = false diff --git a/benches/quantile.rs b/benches/quantile.rs new file mode 100644 index 00000000..b15f8f59 --- /dev/null +++ b/benches/quantile.rs @@ -0,0 +1,108 @@ +extern crate criterion; +extern crate ndarray; +extern crate ndarray_rand; +extern crate ndarray_stats; +extern crate noisy_float; +extern crate rand; + +use criterion::{ + black_box, criterion_group, criterion_main, AxisScale, Criterion, ParameterizedBenchmark, + PlotConfiguration, +}; +use ndarray::prelude::*; +use ndarray_rand::RandomExt; +use ndarray_stats::{interpolate::Midpoint, Quantile1dExt, QuantileExt}; +use noisy_float::types::n64; +use rand::distributions::Uniform; +use rand::random; + +fn quantile_mut(c: &mut Criterion) { + let sizes = vec![10, 100, 1_000, 10_000]; + let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); + c.bench( + "quantile_mut", + ParameterizedBenchmark::new( + "quantile_mut", + |bencher, &size| { + bencher.iter_with_setup( + || { + ( + Array1::random(size, Uniform::new(-50., 50.)).mapv(n64), + random(), + ) + }, + |(mut arr, q)| black_box(arr.quantile_mut::(q)), + ) + }, + sizes, + ) + .plot_config(plot_config), + ); +} + +fn min_and_min_skipnan_without_nans(c: &mut Criterion) { + let sizes = vec![10, 100, 1_000, 10_000]; + let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); + c.bench( + "min_and_min_skipnan_without_nans", + ParameterizedBenchmark::new( + "min", + |bencher, &size| { + bencher.iter_with_setup( + || Array1::random(size, Uniform::new(-50., 50.)), + |arr| black_box(*arr.min().unwrap()), + ) + }, + sizes, + ) + .with_function("min_skipnan", |bencher, &size| { + bencher.iter_with_setup( + || Array1::random(size, Uniform::new(-50., 50.)), + |arr| black_box(*arr.min_skipnan()), + ) + }) + .plot_config(plot_config), + ); +} + +fn quantile_axis_mut(c: &mut Criterion) { + let sizes = vec![10, 100, 1_000]; + let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); + c.bench( + "quantile_axis_mut", + ParameterizedBenchmark::new( + "rowwise", + |bencher, &size| { + bencher.iter_with_setup( + || { + ( + Array2::random((size, 20), Uniform::new(-50., 50.)).mapv(n64), + random(), + ) + }, + |(mut arr, q)| black_box(arr.quantile_axis_mut::(Axis(0), q)), + ) + }, + sizes, + ) + .with_function("columnwise", |bencher, &size| { + bencher.iter_with_setup( + || { + ( + Array2::random((20, size), Uniform::new(-50., 50.)).mapv(n64), + random(), + ) + }, + |(mut arr, q)| black_box(arr.quantile_axis_mut::(Axis(1), q)), + ) + }) + .plot_config(plot_config), + ); +} + +criterion_group! { + name = benches; + config = Criterion::default(); + targets = quantile_mut, min_and_min_skipnan_without_nans, quantile_axis_mut +} +criterion_main!(benches);