1use criterion::{black_box, criterion_group, criterion_main, Criterion};
2use itertools::Itertools;
3
4// Keep aggregate generated elements the same, regardless of powerset length.
5const TOTAL_ELEMENTS: usize = 1 << 12;
6const fn calc_iters(n: usize) -> usize {
7 TOTAL_ELEMENTS / (1 << n)
8}
9
10fn powerset_n(c: &mut Criterion, n: usize) {
11 let id = format!("powerset {}", n);
12 c.bench_function(id.as_str(), move |b| {
13 b.iter(|| {
14 for _ in 0..calc_iters(n) {
15 for elt in (0..n).powerset() {
16 black_box(elt);
17 }
18 }
19 })
20 });
21}
22
23fn powerset_0(c: &mut Criterion) { powerset_n(c, 0); }
24
25fn powerset_1(c: &mut Criterion) { powerset_n(c, 1); }
26
27fn powerset_2(c: &mut Criterion) { powerset_n(c, 2); }
28
29fn powerset_4(c: &mut Criterion) { powerset_n(c, 4); }
30
31fn powerset_8(c: &mut Criterion) { powerset_n(c, 8); }
32
33fn powerset_12(c: &mut Criterion) { powerset_n(c, 12); }
34
35criterion_group!(
36 benches,
37 powerset_0,
38 powerset_1,
39 powerset_2,
40 powerset_4,
41 powerset_8,
42 powerset_12,
43);
44criterion_main!(benches);