Skip to content

Commit 7fead5f

Browse files
committed
Use criterion for benchmarking
1 parent 9167a8b commit 7fead5f

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

Cargo.toml

+15-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,21 @@ A package for calculating primes using the Sieve of Eratosthenes, and using that
1313
number is prime and calculating factors. Includes an iterator over all primes.
1414
"""
1515

16-
readme="README.md"
16+
readme = "README.md"
1717

1818
documentation = "https://wackywendell.github.io/primes/"
1919
homepage = "https://github.com/wackywendell/primes/tree/master"
20-
repository = "https://github.com/wackywendell/primes/tree/master"
20+
repository = "https://github.com/wackywendell/primes/tree/master"
21+
22+
[lib]
23+
# Disable benches in the normal library, we don't need them
24+
# Without disabling this, running cargo bench -- --something-else gets somehow passed to both
25+
# the cargo bench binary and the compiled benches binary
26+
bench = false
27+
28+
[dev-dependencies]
29+
criterion = "0.3.1"
30+
31+
[[bench]]
32+
name = "benches"
33+
harness = false

benches/benches.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
#![feature(test)]
2-
3-
extern crate test;
1+
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
42

53
use primes::PrimeSet;
6-
use test::Bencher;
74

8-
#[bench]
9-
fn bench_primes(b: &mut Bencher) {
10-
b.iter(|| {
11-
let mut pset = PrimeSet::new();
12-
let (_, _) = pset.find(1_000_000);
13-
//~ let (idx, n) = pset.find(1_000_000);
14-
//~ println!("Prime {}: {}", idx, n);
15-
})
5+
fn bench_primes(c: &mut Criterion) {
6+
let mut group = c.benchmark_group("PrimeSet::find");
7+
for size in [
8+
100, 200, 500, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000, 200_000, 500_000,
9+
]
10+
.iter()
11+
{
12+
group.throughput(Throughput::Elements(*size as u64));
13+
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
14+
b.iter(|| {
15+
let mut pset = PrimeSet::new();
16+
black_box(pset.find(size))
17+
})
18+
});
19+
}
20+
group.finish();
1621
}
22+
23+
criterion_group!(benches, bench_primes);
24+
criterion_main!(benches);

0 commit comments

Comments
 (0)