summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bit-vec/benches/bench.rs
blob: f859c410abf6191fb173c2880488650f417c6ea0 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// 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;
}

#[bench]
fn bench_erathostenes(b: &mut test::Bencher) {
    let mut primes = vec![];
    b.iter(|| {
        primes.clear();
        let mut sieve = BitVec::from_elem(1 << 16, true);
        black_box(&mut sieve);
        let mut i = 2;
        while i < sieve.len() {
            if sieve[i] == true {
                primes.push(i);
            }
            let mut j = i;
            while j < sieve.len() {
                sieve.set(j, false);
                j += i;
            }
            i += 1;
        }
        black_box(&mut sieve);
    });
}

#[bench]
fn bench_erathostenes_set_all(b: &mut test::Bencher) {
    let mut primes = vec![];
    let mut sieve = BitVec::from_elem(1 << 16, true);
    b.iter(|| {
        primes.clear();
        black_box(&mut sieve);
        sieve.set_all();
        black_box(&mut sieve);
        let mut i = 2;
        while i < sieve.len() {
            if sieve[i] == true {
                primes.push(i);
            }
            let mut j = i;
            while j < sieve.len() {
                sieve.set(j, false);
                j += i;
            }
            i += 1;
        }
        black_box(&mut sieve);
    });
}