diff --git a/benches/lib.rs b/benches/lib.rs index d1ae07d..04806d3 100644 --- a/benches/lib.rs +++ b/benches/lib.rs @@ -1,31 +1,42 @@ #![feature(test)] -extern crate md5; extern crate test; -#[bench] -fn compute_0001000(bencher: &mut test::Bencher) { - compute(1000, bencher); -} +use md5::Context; -#[bench] -fn compute_0010000(bencher: &mut test::Bencher) { - compute(10000, bencher); -} +macro_rules! implement( + ($($size:literal => ($compute:ident, $context:ident),)*) => ($( + #[bench] + fn $compute(bencher: &mut test::Bencher) { + compute($size, bencher); + } -#[bench] -fn compute_0100000(bencher: &mut test::Bencher) { - compute(100000, bencher); -} + #[bench] + fn $context(bencher: &mut test::Bencher) { + context($size, bencher); + } + )*); +); -#[bench] -fn compute_1000000(bencher: &mut test::Bencher) { - compute(1000000, bencher); +implement! { + 1000 => (compute_0001000, context_0001000), + 10000 => (compute_0010000, context_0010000), + 100000 => (compute_0100000, context_0100000), + 1000000 => (compute_1000000, context_1000000), } fn compute(size: usize, bencher: &mut test::Bencher) { - let data = &vec![0xffu8; size][..]; + let data = vec![0xffu8; size]; + bencher.iter(|| { + test::black_box(md5::compute(&data)); + }); +} + +fn context(size: usize, bencher: &mut test::Bencher) { + let data = vec![0xffu8; size]; bencher.iter(|| { - test::black_box(md5::compute(data)); + let mut context = Context::new(); + context.consume(&data); + test::black_box(context.compute()); }); }