|
| 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