blob: 9a9edecec6246029a0cf58758a688e8c5750b106 (
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
|
#![feature(test)]
extern crate test;
use test::Bencher;
use clint::Handler;
const ITER_COUNT: usize = 10_000;
#[bench]
fn bench_bare_fn(b: &mut Bencher) {
static mut X: usize = 0;
#[inline(never)]
fn inc() {
unsafe { X += 1 };
}
let n = test::black_box(ITER_COUNT);
b.iter(|| (0..n).for_each(|_| inc()));
assert!(unsafe { X } > 0);
}
#[bench]
fn bench_handler(b: &mut Bencher) {
static mut X: usize = 0;
let mut handler = Handler::new();
unsafe { handler.replace(&move || X += 1) };
let n = test::black_box(ITER_COUNT);
b.iter(|| (0..n).for_each(|_| unsafe { handler.call() }));
assert!(unsafe { X } > 0);
}
|