diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/bit-vec/benches | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/bit-vec/benches')
-rw-r--r-- | third_party/rust/bit-vec/benches/bench.rs | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/third_party/rust/bit-vec/benches/bench.rs b/third_party/rust/bit-vec/benches/bench.rs new file mode 100644 index 0000000000..c4137591e7 --- /dev/null +++ b/third_party/rust/bit-vec/benches/bench.rs @@ -0,0 +1,164 @@ +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(test)] + +extern crate test; +extern crate rand; +extern crate rand_xorshift; +extern crate bit_vec; + +use test::{Bencher, black_box}; +use rand::{Rng, RngCore, SeedableRng}; +use rand_xorshift::XorShiftRng; +use bit_vec::BitVec; + +const HUGE_BENCH_BITS : usize = 1 << 20; +const BENCH_BITS : usize = 1 << 14; +const U32_BITS: usize = 32; + +fn small_rng() -> XorShiftRng { + XorShiftRng::from_entropy() +} + +#[bench] +fn bench_usize_small(b: &mut Bencher) { + let mut r = small_rng(); + let mut bit_vec = 0 as usize; + b.iter(|| { + for _ in 0..100 { + bit_vec |= 1 << ((r.next_u32() as usize) % U32_BITS); + } + black_box(&bit_vec); + }); +} + +#[bench] +fn bench_bit_set_big_fixed(b: &mut Bencher) { + let mut r = small_rng(); + let mut bit_vec = BitVec::from_elem(BENCH_BITS, false); + b.iter(|| { + for _ in 0..100 { + bit_vec.set((r.next_u32() as usize) % BENCH_BITS, true); + } + black_box(&bit_vec); + }); +} + +#[bench] +fn bench_bit_set_big_variable(b: &mut Bencher) { + let mut r = small_rng(); + let mut bit_vec = BitVec::from_elem(BENCH_BITS, false); + b.iter(|| { + for _ in 0..100 { + bit_vec.set((r.next_u32() as usize) % BENCH_BITS, r.gen()); + } + black_box(&bit_vec); + }); +} + +#[bench] +fn bench_bit_set_small(b: &mut Bencher) { + let mut r = small_rng(); + let mut bit_vec = BitVec::from_elem(U32_BITS, false); + b.iter(|| { + for _ in 0..100 { + bit_vec.set((r.next_u32() as usize) % U32_BITS, true); + } + black_box(&bit_vec); + }); +} + +#[bench] +fn bench_bit_vec_big_or(b: &mut Bencher) { + let mut b1 = BitVec::from_elem(BENCH_BITS, false); + let b2 = BitVec::from_elem(BENCH_BITS, false); + b.iter(|| { + b1.or(&b2) + }) +} + +#[bench] +fn bench_bit_vec_big_xnor(b: &mut Bencher) { + let mut b1 = BitVec::from_elem(BENCH_BITS, false); + let b2 = BitVec::from_elem(BENCH_BITS, false); + b.iter(|| { + b1.xnor(&b2) + }) +} + +#[bench] +fn bench_bit_vec_big_negate_xor(b: &mut Bencher) { + let mut b1 = BitVec::from_elem(BENCH_BITS, false); + let b2 = BitVec::from_elem(BENCH_BITS, false); + b.iter(|| { + let res = b1.xor(&b2); + b1.negate(); + res + }) +} + +#[bench] +fn bench_bit_vec_huge_xnor(b: &mut Bencher) { + let mut b1 = BitVec::from_elem(HUGE_BENCH_BITS, false); + let b2 = BitVec::from_elem(HUGE_BENCH_BITS, false); + b.iter(|| { + b1.xnor(&b2) + }) +} + +#[bench] +fn bench_bit_vec_huge_negate_xor(b: &mut Bencher) { + let mut b1 = BitVec::from_elem(HUGE_BENCH_BITS, false); + let b2 = BitVec::from_elem(HUGE_BENCH_BITS, false); + b.iter(|| { + let res = b1.xor(&b2); + b1.negate(); + res + }) +} + +#[bench] +fn bench_bit_vec_small_iter(b: &mut Bencher) { + let bit_vec = BitVec::from_elem(U32_BITS, false); + b.iter(|| { + let mut sum = 0; + for _ in 0..10 { + for pres in &bit_vec { + sum += pres as usize; + } + } + sum + }) +} + +#[bench] +fn bench_bit_vec_big_iter(b: &mut Bencher) { + let bit_vec = BitVec::from_elem(BENCH_BITS, false); + b.iter(|| { + let mut sum = 0; + for pres in &bit_vec { + sum += pres as usize; + } + sum + }) +} + +#[bench] +fn bench_from_elem(b: &mut Bencher) { + let cap = black_box(BENCH_BITS); + let bit = black_box(true); + b.iter(|| { + // create a BitVec and popcount it + BitVec::from_elem(cap, bit).blocks() + .fold(0, |acc, b| acc + b.count_ones()) + }); + b.bytes = cap as u64 / 8; +} |