1 | use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
2 | use itertools::Itertools; |
3 | |
4 | // Keep aggregate generated elements the same, regardless of powerset length. |
5 | const TOTAL_ELEMENTS: usize = 1 << 12; |
6 | const fn calc_iters(n: usize) -> usize { |
7 | TOTAL_ELEMENTS / (1 << n) |
8 | } |
9 | |
10 | fn 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 | |
23 | fn powerset_0(c: &mut Criterion) { powerset_n(c, 0); } |
24 | |
25 | fn powerset_1(c: &mut Criterion) { powerset_n(c, 1); } |
26 | |
27 | fn powerset_2(c: &mut Criterion) { powerset_n(c, 2); } |
28 | |
29 | fn powerset_4(c: &mut Criterion) { powerset_n(c, 4); } |
30 | |
31 | fn powerset_8(c: &mut Criterion) { powerset_n(c, 8); } |
32 | |
33 | fn powerset_12(c: &mut Criterion) { powerset_n(c, 12); } |
34 | |
35 | criterion_group!( |
36 | benches, |
37 | powerset_0, |
38 | powerset_1, |
39 | powerset_2, |
40 | powerset_4, |
41 | powerset_8, |
42 | powerset_12, |
43 | ); |
44 | criterion_main!(benches); |