-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbasic_example.rs
More file actions
69 lines (54 loc) · 1.4 KB
/
basic_example.rs
File metadata and controls
69 lines (54 loc) · 1.4 KB
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
use codspeed_divan_compat::Bencher;
fn fibo(n: u64) -> u64 {
let mut a = 0;
let mut b = 1;
for _ in 0..n {
let tmp = a;
a = b;
b += tmp;
}
a
}
#[codspeed_divan_compat::bench]
fn fibo_50() -> u64 {
codspeed_divan_compat::black_box(fibo(50))
}
#[codspeed_divan_compat::bench]
fn fibo_10() -> u64 {
codspeed_divan_compat::black_box(fibo(10))
}
#[codspeed_divan_compat::bench]
fn mut_borrow(bencher: Bencher) {
let mut bytes = Vec::<i32>::new();
bencher.bench_local(|| {
bytes.push(42);
});
}
// Examples taken from the docs: https://docs.rs/divan/latest/divan/attr.bench.html#consts
mod const_bench {
const LEN: usize = 42;
const fn len() -> usize {
4
}
#[codspeed_divan_compat::bench(consts = [1000, LEN, len()])]
fn init_array<const N: usize>() -> [i32; N] {
let mut result = [0; N];
#[allow(clippy::needless_range_loop)]
for i in 0..N {
result[i] = divan::black_box(i as i32);
}
result
}
const SIZES: &[usize] = &[1, 10, LEN, len()];
#[codspeed_divan_compat::bench(consts = SIZES)]
fn bench_array1<const N: usize>() -> [i32; N] {
init_array::<N>()
}
#[codspeed_divan_compat::bench(consts = SIZES)]
fn bench_array2<const N: usize>() -> [i32; N] {
init_array::<N>()
}
}
fn main() {
codspeed_divan_compat::main();
}