Skip to content

Commit

Permalink
Convert benchmarks to criterion.
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw committed Dec 8, 2018
1 parent 19a65a7 commit f0828b8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 79 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ unstable = []
[dev-dependencies]
quickcheck = "0.2"
rand = "0.3"
criterion = "0.2"

[[bench]]
name = "benches"
harness = false
74 changes: 74 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#[macro_use]
extern crate criterion;
extern crate hamming;

use criterion::{Criterion, Bencher, ParameterizedBenchmark, PlotConfiguration, AxisScale};

const SIZES: [usize; 7] = [1, 10, 100, 1000, 10_000, 100_000, 1_000_000];

macro_rules! create_benchmarks {
($(
fn $group_id: ident($input: expr) {
$first_name: expr => $first_func: expr,
$($rest_name: expr => $rest_func: expr,)*
}
)*) => {
$(
fn $group_id(c: &mut Criterion) {
let input = $input;

let plot_config =
PlotConfiguration::default()
.summary_scale(AxisScale::Logarithmic);
let bench = ParameterizedBenchmark::new(
$first_name, $first_func, input.into_iter().cloned())
$( .with_function($rest_name, $rest_func) )*
.plot_config(plot_config);
c.bench(stringify!($group_id), bench);
}
)*
}
}

fn naive_weight(x: &[u8]) -> u64 {
x.iter().fold(0, |a, b| a + b.count_ones() as u64)
}

fn weight_bench<F: 'static + FnMut(&[u8]) -> u64>(mut f: F) -> impl FnMut(&mut Bencher, &usize) {
move |b, n| {
let data = vec![0xFF; *n];
b.iter(|| f(criterion::black_box(&data)))
}
}

fn naive_distance(x: &[u8], y: &[u8]) -> u64 {
assert_eq!(x.len(), y.len());
x.iter().zip(y).fold(0, |a, (b, c)| a + (*b ^ *c).count_ones() as u64)
}

fn distance_bench<F: 'static + FnMut(&[u8], &[u8]) -> u64>(mut f: F) -> impl FnMut(&mut Bencher, &usize) {
move |b, n| {
let data = vec![0xFF; *n];
b.iter(|| {
let d1 = criterion::black_box(&data);
let d2 = criterion::black_box(&data);
f(d1, d2)
})
}
}



create_benchmarks! {
fn weight(SIZES) {
"naive" => weight_bench(naive_weight),
"weight" => weight_bench(hamming::weight),
}
fn distance(SIZES) {
"naive" => distance_bench(naive_distance),
"distance" => distance_bench(hamming::distance),
}
}

criterion_group!(benches, weight, distance);
criterion_main!(benches);
43 changes: 0 additions & 43 deletions src/distance_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ pub fn distance(x: &[u8], y: &[u8]) -> u64 {
.unwrap_or_else(|| naive(x, y))
}

#[cfg(all(test, feature = "unstable"))]
fn distance_(x: &[u8], y: &[u8]) -> u64 {
distance_fast(x, y).unwrap()
}

#[cfg(test)]
mod tests {
use quickcheck as qc;
Expand Down Expand Up @@ -254,41 +249,3 @@ mod tests {
}
}
}

#[cfg(all(test, feature = "unstable"))]
mod benches {
use test;
fn bench<F: FnMut(&[u8], &[u8]) -> u64>(b: &mut test::Bencher, n: usize, mut f: F) {
let data = vec![0xFF; n];
b.iter(|| {
let d1 = test::black_box(&data);
let d2 = test::black_box(&data);
f(d1, d2)
})
}
macro_rules! test_mod {
($name: ident) => {
mod $name {
use test;
use super::bench;
use super::super::$name;
#[bench]
fn _0000001(b: &mut test::Bencher) { bench(b, 1, $name) }
#[bench]
fn _0000010(b: &mut test::Bencher) { bench(b, 10, $name) }
#[bench]
fn _0000100(b: &mut test::Bencher) { bench(b, 100, $name) }
#[bench]
fn _0001000(b: &mut test::Bencher) { bench(b, 1000, $name) }
#[bench]
fn _0010000(b: &mut test::Bencher) { bench(b, 10000, $name) }
#[bench]
fn _0100000(b: &mut test::Bencher) { bench(b, 100000, $name) }
#[bench]
fn _1000000(b: &mut test::Bencher) { bench(b, 1000000, $name) }
}
}
}
test_mod!(naive);
test_mod!(distance_);
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
//! assert_eq!(hamming::distance(&[1, 0xFF], &[0xFF, 1]), 7 + 7);
//! ```

#![cfg_attr(all(test, feature = "unstable"), feature(test))]
#[cfg(test)] extern crate quickcheck;
#[cfg(test)] extern crate rand;
#[cfg(all(test, feature = "unstable"))] extern crate test;

mod weight_;
pub use weight_::weight;
Expand Down
34 changes: 0 additions & 34 deletions src/weight_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,37 +129,3 @@ mod tests {
v[0].count_ones() as u64 * v.len() as u64);
}
}

#[cfg(all(test, feature = "unstable"))]
mod benches {
use test;
fn bench<F: FnMut(&[u8]) -> u64>(b: &mut test::Bencher, n: usize, mut f: F) {
let data = vec![0xFF; n];
b.iter(|| f(test::black_box(&data)))
}
macro_rules! test_mod {
($name: ident) => {
mod $name {
use test;
use super::bench;
use super::super::$name;
#[bench]
fn _0000001(b: &mut test::Bencher) { bench(b, 1, $name) }
#[bench]
fn _0000010(b: &mut test::Bencher) { bench(b, 10, $name) }
#[bench]
fn _0000100(b: &mut test::Bencher) { bench(b, 100, $name) }
#[bench]
fn _0001000(b: &mut test::Bencher) { bench(b, 1000, $name) }
#[bench]
fn _0010000(b: &mut test::Bencher) { bench(b, 10000, $name) }
#[bench]
fn _0100000(b: &mut test::Bencher) { bench(b, 100000, $name) }
#[bench]
fn _1000000(b: &mut test::Bencher) { bench(b, 1000000, $name) }
}
}
}
test_mod!(naive);
test_mod!(weight);
}

0 comments on commit f0828b8

Please sign in to comment.