From 7fead5fb0bff6947a9460ebfb867522799e58717 Mon Sep 17 00:00:00 2001 From: Wendell Smith Date: Sat, 7 Mar 2020 15:34:01 -0500 Subject: [PATCH] Use criterion for benchmarking --- Cargo.toml | 17 +++++++++++++++-- benches/benches.rs | 32 ++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ae5cc6..bcbe3a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,21 @@ A package for calculating primes using the Sieve of Eratosthenes, and using that number is prime and calculating factors. Includes an iterator over all primes. """ -readme="README.md" +readme = "README.md" documentation = "https://wackywendell.github.io/primes/" homepage = "https://github.com/wackywendell/primes/tree/master" -repository = "https://github.com/wackywendell/primes/tree/master" \ No newline at end of file +repository = "https://github.com/wackywendell/primes/tree/master" + +[lib] +# Disable benches in the normal library, we don't need them +# Without disabling this, running cargo bench -- --something-else gets somehow passed to both +# the cargo bench binary and the compiled benches binary +bench = false + +[dev-dependencies] +criterion = "0.3.1" + +[[bench]] +name = "benches" +harness = false \ No newline at end of file diff --git a/benches/benches.rs b/benches/benches.rs index 2e3afff..8045628 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -1,16 +1,24 @@ -#![feature(test)] - -extern crate test; +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; use primes::PrimeSet; -use test::Bencher; -#[bench] -fn bench_primes(b: &mut Bencher) { - b.iter(|| { - let mut pset = PrimeSet::new(); - let (_, _) = pset.find(1_000_000); - //~ let (idx, n) = pset.find(1_000_000); - //~ println!("Prime {}: {}", idx, n); - }) +fn bench_primes(c: &mut Criterion) { + let mut group = c.benchmark_group("PrimeSet::find"); + for size in [ + 100, 200, 500, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000, 200_000, 500_000, + ] + .iter() + { + group.throughput(Throughput::Elements(*size as u64)); + group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| { + b.iter(|| { + let mut pset = PrimeSet::new(); + black_box(pset.find(size)) + }) + }); + } + group.finish(); } + +criterion_group!(benches, bench_primes); +criterion_main!(benches);