Skip to content

Commit 7d5ca01

Browse files
authored
Use Criterion for benchmarks (#84)
1 parent d13ea5c commit 7d5ca01

4 files changed

Lines changed: 204 additions & 132 deletions

File tree

.github/workflows/rust.yml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ env:
1111
CARGO_INCREMENTAL: 0
1212

1313
jobs:
14+
# Ensure the crate builds
1415
build:
1516

1617
runs-on: ubuntu-latest
@@ -32,6 +33,7 @@ jobs:
3233
cargo test --verbose --features "$FEATURES" &&
3334
cargo test --verbose --release --features "$FEATURES"
3435
36+
# Use clippy to lint for code smells
3537
clippy:
3638

3739
runs-on: ubuntu-latest
@@ -52,6 +54,7 @@ jobs:
5254
run: |
5355
cargo clippy
5456
57+
# Enforce rustfmt formatting
5558
formatting:
5659

5760
runs-on: ubuntu-latest
@@ -70,4 +73,26 @@ jobs:
7073
override: true
7174
- name: Run Clippy
7275
run: |
73-
cargo fmt --all --check
76+
cargo fmt --all --check
77+
78+
# Ensure the benchmarks compile
79+
benchmark_compiles:
80+
81+
runs-on: ubuntu-latest
82+
strategy:
83+
matrix:
84+
# Check builds only on stable
85+
rust: [stable]
86+
87+
steps:
88+
- uses: actions/checkout@v2
89+
- uses: dtolnay/rust-toolchain@stable
90+
with:
91+
profile: minimal
92+
toolchain: ${{ matrix.rust }}
93+
components: clippy
94+
override: true
95+
- name: Run Clippy
96+
run: |
97+
cd benches
98+
cargo bench --bench benches --no-run

benches/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "benches"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Benchmarks for FixedBitset"
6+
publish = false
7+
license = "MIT OR Apache-2.0"
8+
9+
[dev-dependencies]
10+
fixedbitset = { path = ".." }
11+
criterion = { version = "0.4", features = ["html_reports"] }
12+
13+
[[bench]]
14+
name = "benches"
15+
harness = false

benches/benches.rs

Lines changed: 0 additions & 131 deletions
This file was deleted.

benches/benches/benches.rs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
extern crate criterion;
2+
extern crate fixedbitset;
3+
use criterion::{criterion_group, criterion_main, Criterion};
4+
use fixedbitset::FixedBitSet;
5+
use std::hint::black_box;
6+
7+
#[inline]
8+
fn iter_ones_using_contains<F: FnMut(usize)>(fb: &FixedBitSet, f: &mut F) {
9+
for bit in 0..fb.len() {
10+
if fb.contains(bit) {
11+
f(bit);
12+
}
13+
}
14+
}
15+
16+
fn iter_ones_using_contains_all_zeros(c: &mut Criterion) {
17+
const N: usize = 1_000_000;
18+
let fb = FixedBitSet::with_capacity(N);
19+
20+
c.bench_function("iter_ones/contains_all_zeros", |b| {
21+
b.iter(|| {
22+
let mut count = 0;
23+
iter_ones_using_contains(&fb, &mut |_bit| count += 1);
24+
count
25+
})
26+
});
27+
}
28+
29+
fn iter_ones_using_contains_all_ones(c: &mut Criterion) {
30+
const N: usize = 1_000_000;
31+
let mut fb = FixedBitSet::with_capacity(N);
32+
fb.insert_range(..);
33+
34+
c.bench_function("iter_ones/contains_all_ones", |b| {
35+
b.iter(|| {
36+
let mut count = 0;
37+
iter_ones_using_contains(&fb, &mut |_bit| count += 1);
38+
count
39+
})
40+
});
41+
}
42+
43+
fn iter_ones_all_zeros(c: &mut Criterion) {
44+
const N: usize = 1_000_000;
45+
let fb = FixedBitSet::with_capacity(N);
46+
47+
c.bench_function("iter_ones/all_zeros", |b| {
48+
b.iter(|| {
49+
let mut count = 0;
50+
for _ in fb.ones() {
51+
count += 1;
52+
}
53+
count
54+
})
55+
});
56+
}
57+
58+
fn iter_ones_all_ones(c: &mut Criterion) {
59+
const N: usize = 1_000_000;
60+
let mut fb = FixedBitSet::with_capacity(N);
61+
fb.insert_range(..);
62+
63+
c.bench_function("iter_ones/all_ones", |b| {
64+
b.iter(|| {
65+
let mut count = 0;
66+
for _ in fb.ones() {
67+
count += 1;
68+
}
69+
count
70+
})
71+
});
72+
}
73+
74+
fn insert_range(c: &mut Criterion) {
75+
const N: usize = 1_000_000;
76+
let mut fb = FixedBitSet::with_capacity(N);
77+
78+
c.bench_function("insert_range/1m", |b| b.iter(|| fb.insert_range(..)));
79+
}
80+
81+
fn insert(c: &mut Criterion) {
82+
const N: usize = 1_000_000;
83+
let mut fb = FixedBitSet::with_capacity(N);
84+
85+
c.bench_function("insert/1m", |b| {
86+
b.iter(|| {
87+
for i in 0..N {
88+
fb.insert(i);
89+
}
90+
})
91+
});
92+
}
93+
94+
fn union_with(c: &mut Criterion) {
95+
const N: usize = 1_000_000;
96+
let mut fb_a = FixedBitSet::with_capacity(N);
97+
let fb_b = FixedBitSet::with_capacity(N);
98+
99+
c.bench_function("union_with/1m", |b| b.iter(|| fb_a.union_with(&fb_b)));
100+
}
101+
102+
fn intersect_with(c: &mut Criterion) {
103+
const N: usize = 1_000_000;
104+
let mut fb_a = FixedBitSet::with_capacity(N);
105+
let fb_b = FixedBitSet::with_capacity(N);
106+
107+
c.bench_function("intersect_with/1m", |b| {
108+
b.iter(|| fb_a.intersect_with(&fb_b))
109+
});
110+
}
111+
112+
fn difference_with(c: &mut Criterion) {
113+
const N: usize = 1_000_000;
114+
let mut fb_a = FixedBitSet::with_capacity(N);
115+
let fb_b = FixedBitSet::with_capacity(N);
116+
117+
c.bench_function("difference_with/1m", |b| {
118+
b.iter(|| fb_a.difference_with(&fb_b))
119+
});
120+
}
121+
122+
fn symmetric_difference_with(c: &mut Criterion) {
123+
const N: usize = 1_000_000;
124+
let mut fb_a = FixedBitSet::with_capacity(N);
125+
let fb_b = FixedBitSet::with_capacity(N);
126+
127+
c.bench_function("symmetric_difference_with/1m", |b| {
128+
b.iter(|| fb_a.symmetric_difference_with(&fb_b))
129+
});
130+
}
131+
132+
fn clear(c: &mut Criterion) {
133+
const N: usize = 1_000_000;
134+
let mut fb_a = FixedBitSet::with_capacity(N);
135+
136+
c.bench_function("clear/1m", |b| b.iter(|| fb_a.clear()));
137+
}
138+
139+
fn count_ones(c: &mut Criterion) {
140+
const N: usize = 1_000_000;
141+
let fb_a = FixedBitSet::with_capacity(N);
142+
143+
c.bench_function("count_ones/1m", |b| {
144+
b.iter(|| black_box(fb_a.count_ones(..)))
145+
});
146+
}
147+
148+
criterion_group!(
149+
benches,
150+
iter_ones_using_contains_all_zeros,
151+
iter_ones_using_contains_all_ones,
152+
iter_ones_all_zeros,
153+
iter_ones_all_ones,
154+
insert_range,
155+
insert,
156+
intersect_with,
157+
difference_with,
158+
union_with,
159+
symmetric_difference_with,
160+
count_ones,
161+
clear,
162+
);
163+
criterion_main!(benches);

0 commit comments

Comments
 (0)