111 lines
2.2 KiB
Rust
111 lines
2.2 KiB
Rust
#[macro_use]
|
|
extern crate criterion;
|
|
extern crate rand;
|
|
|
|
extern crate bit_set;
|
|
|
|
use std::iter::FromIterator;
|
|
|
|
use criterion::Criterion;
|
|
use rand::{Rng, SeedableRng, XorShiftRng};
|
|
|
|
use bit_set::BitSet;
|
|
|
|
fn random<C: FromIterator<u64>>(n: usize) -> C {
|
|
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
|
|
|
|
(0..n).map(|_| rng.next_u64()).collect()
|
|
}
|
|
|
|
fn contains(data: &[u64]) {
|
|
let set = BitSet::new();
|
|
|
|
for v in data {
|
|
set.contains(*v);
|
|
}
|
|
}
|
|
|
|
fn insert(data: &[u64]) {
|
|
let mut set = BitSet::new();
|
|
|
|
for v in data {
|
|
set.insert(*v);
|
|
}
|
|
}
|
|
|
|
fn iter(set: &BitSet) {
|
|
set.iter().collect::<Vec<_>>();
|
|
}
|
|
|
|
fn is_subset(a: &BitSet, b: &BitSet) {
|
|
a.is_subset(b);
|
|
}
|
|
|
|
fn ops_bit_or(a: &BitSet, b: &BitSet) {
|
|
a | b;
|
|
}
|
|
|
|
fn ops_sub(a: &BitSet, b: &BitSet) {
|
|
a - b;
|
|
}
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
// c.sample_size(200);
|
|
|
|
c.bench_function("contains", move |b| {
|
|
let data: Vec<_> = random(10_000);
|
|
|
|
b.iter(|| contains(&data));
|
|
});
|
|
|
|
c.bench_function("insert", move |b| {
|
|
let data: Vec<_> = random(10_000);
|
|
|
|
b.iter(|| insert(&data));
|
|
});
|
|
|
|
c.bench_function("iter", move |b| {
|
|
let set: BitSet = random(10_000);
|
|
|
|
b.iter(|| iter(&set));
|
|
});
|
|
|
|
c.bench_function("is_subset (>)", |b| {
|
|
let lhs: BitSet = random(10_000);
|
|
let rhs: BitSet = random(5_000);
|
|
|
|
b.iter(|| is_subset(&lhs, &rhs));
|
|
});
|
|
|
|
c.bench_function("is_subset (<)", |b| {
|
|
let lhs: BitSet = random(5_000);
|
|
let rhs: BitSet = random(10_000);
|
|
|
|
b.iter(|| is_subset(&lhs, &rhs));
|
|
});
|
|
|
|
c.bench_function("is_subset (=)", |b| {
|
|
let lhs: BitSet = random(10_000);
|
|
let rhs: BitSet = random(10_000);
|
|
|
|
b.iter(|| is_subset(&lhs, &rhs));
|
|
});
|
|
|
|
c.bench_function("ops_bit_or", move |b| {
|
|
let lhs: BitSet = random(10_000);
|
|
let rhs: BitSet = random(10_000);
|
|
|
|
b.iter(|| ops_bit_or(&lhs, &rhs));
|
|
});
|
|
|
|
c.bench_function("ops_sub", move |b| {
|
|
let lhs: BitSet = random(10_000);
|
|
let rhs: BitSet = random(10_000);
|
|
|
|
b.iter(|| ops_sub(&lhs, &rhs));
|
|
});
|
|
}
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
criterion_main!(benches);
|