summaryrefslogtreecommitdiff
path: root/benches/benches.rs
blob: ac999b014a4e683104c4141f88badfee19cb3671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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);
        }
    });
}