diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/fixedbitset/benches | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/fixedbitset/benches')
-rw-r--r-- | vendor/fixedbitset/benches/benches.rs | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/vendor/fixedbitset/benches/benches.rs b/vendor/fixedbitset/benches/benches.rs new file mode 100644 index 000000000..ac999b014 --- /dev/null +++ b/vendor/fixedbitset/benches/benches.rs @@ -0,0 +1,133 @@ +#![feature(test)] + +extern crate test; +extern crate fixedbitset; +use test::Bencher; +use fixedbitset::{FixedBitSet}; +use std::mem::size_of; + +#[inline] +fn iter_ones_using_contains<F: FnMut(usize)>(fb: &FixedBitSet, f: &mut F) { + for bit in 0 .. fb.len() { + if fb.contains(bit) { + f(bit); + } + } +} + +#[inline] +fn iter_ones_using_slice_directly<F: FnMut(usize)>(fb: &FixedBitSet, f: &mut F) { + for (block_idx, &block) in fb.as_slice().iter().enumerate() { + let mut bit_pos = block_idx * size_of::<u32>() * 8; + let mut block: u32 = block; + + while block != 0 { + if (block & 1) == 1 { + f(bit_pos); + } + block = block >> 1; + bit_pos += 1; + } + } +} + +#[bench] +fn bench_iter_ones_using_contains_all_zeros(b: &mut Bencher) { + const N: usize = 1_000_000; + let fb = FixedBitSet::with_capacity(N); + + b.iter(|| { + let mut count = 0; + iter_ones_using_contains(&fb, &mut |_bit| count += 1); + count + }); +} + +#[bench] +fn bench_iter_ones_using_contains_all_ones(b: &mut Bencher) { + const N: usize = 1_000_000; + let mut fb = FixedBitSet::with_capacity(N); + fb.insert_range(..); + + b.iter(|| { + let mut count = 0; + iter_ones_using_contains(&fb, &mut |_bit| count += 1); + count + }); +} + +#[bench] +fn bench_iter_ones_using_slice_directly_all_zero(b: &mut Bencher) { + const N: usize = 1_000_000; + let fb = FixedBitSet::with_capacity(N); + + b.iter(|| { + let mut count = 0; + iter_ones_using_slice_directly(&fb, &mut |_bit| count += 1); + count + }); +} + +#[bench] +fn bench_iter_ones_using_slice_directly_all_ones(b: &mut Bencher) { + const N: usize = 1_000_000; + let mut fb = FixedBitSet::with_capacity(N); + fb.insert_range(..); + + b.iter(|| { + let mut count = 0; + iter_ones_using_slice_directly(&fb, &mut |_bit| count += 1); + count + }); +} + +#[bench] +fn bench_iter_ones_all_zeros(b: &mut Bencher) { + const N: usize = 1_000_000; + let fb = FixedBitSet::with_capacity(N); + + b.iter(|| { + let mut count = 0; + for _ in fb.ones() { + count += 1; + } + count + }); +} + +#[bench] +fn bench_iter_ones_all_ones(b: &mut Bencher) { + const N: usize = 1_000_000; + let mut fb = FixedBitSet::with_capacity(N); + fb.insert_range(..); + + b.iter(|| { + let mut count = 0; + for _ in fb.ones() { + count += 1; + } + count + }); +} + +#[bench] +fn bench_insert_range(b: &mut Bencher) { + const N: usize = 1_000_000; + let mut fb = FixedBitSet::with_capacity(N); + + b.iter(|| { + fb.insert_range(..) + }); +} + +#[bench] +fn bench_insert_range_using_loop(b: &mut Bencher) { + const N: usize = 1_000_000; + let mut fb = FixedBitSet::with_capacity(N); + + b.iter(|| { + for i in 0..N { + fb.insert(i); + } + }); +} |