-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmemchr.rs
121 lines (100 loc) · 3.71 KB
/
wmemchr.rs
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
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use wmemchr_benches::unrolled_find_u16s;
const SMALL: usize = 256;
const MEDIUM: usize = 66536;
const fn middle(n: usize) -> usize {
n * 3 / 4
}
fn bench_small_no_null(c: &mut Criterion) {
let mut rng = rand::thread_rng();
// exclude NUL byte from 1..256 range
let buf: Vec<u16> = (0..SMALL).map(|_| rng.gen_range(1, 256) as u16).collect();
let mut group = c.benchmark_group("small slice [no null in slice]");
group.bench_function("iter::position", |b| {
b.iter(|| buf.iter().position(|&c| c == 0))
});
group.bench_function("unrolled_find_u16s", |b| {
b.iter(|| unrolled_find_u16s(0, &buf))
});
group.finish();
}
fn bench_small_null_middle(c: &mut Criterion) {
let mut rng = rand::thread_rng();
// exclude NUL byte from 1..256 range
let mut buf: Vec<u16> = (0..SMALL).map(|_| rng.gen_range(1, 256) as u16).collect();
buf[middle(SMALL)] = 0;
let mut group = c.benchmark_group("small slice [null in the middle of slice]");
group.bench_function("iter::position", |b| {
b.iter(|| buf.iter().position(|&c| c == 0))
});
group.bench_function("unrolled_find_u16s", |b| {
b.iter(|| unrolled_find_u16s(0, &buf))
});
group.finish();
}
fn bench_small_null_end(c: &mut Criterion) {
let mut rng = rand::thread_rng();
// exclude NUL byte from 1..256 range
let mut buf: Vec<u16> = (0..SMALL).map(|_| rng.gen_range(1, 256) as u16).collect();
buf[SMALL - 1] = 0;
let mut group = c.benchmark_group("small slice [null at the end of slice]");
group.bench_function("iter::position", |b| {
b.iter(|| buf.iter().position(|&c| c == 0))
});
group.bench_function("unrolled_find_u16s", |b| {
b.iter(|| unrolled_find_u16s(0, &buf))
});
group.finish();
}
fn bench_medium_no_null(c: &mut Criterion) {
let mut rng = rand::thread_rng();
// exclude NUL byte from 1..256 range
let buf: Vec<u16> = (0..MEDIUM).map(|_| rng.gen_range(1, 256) as u16).collect();
let mut group = c.benchmark_group("[medium slice [no null in slice]");
group.bench_function("iter::position", |b| {
b.iter(|| buf.iter().position(|&c| c == 0))
});
group.bench_function("unrolled_find_u16s", |b| {
b.iter(|| unrolled_find_u16s(0, &buf))
});
group.finish();
}
fn bench_medium_null_middle(c: &mut Criterion) {
let mut rng = rand::thread_rng();
// exclude NUL byte from 1..256 range
let mut buf: Vec<u16> = (0..MEDIUM).map(|_| rng.gen_range(1, 256) as u16).collect();
buf[middle(MEDIUM)] = 0;
let mut group = c.benchmark_group("medium slice [null in the middle of slice]");
group.bench_function("iter::position", |b| {
b.iter(|| buf.iter().position(|&c| c == 0))
});
group.bench_function("unrolled_find_u16s", |b| {
b.iter(|| unrolled_find_u16s(0, &buf))
});
group.finish();
}
fn bench_medium_null_end(c: &mut Criterion) {
let mut rng = rand::thread_rng();
// exclude NUL byte from 1..256 range
let mut buf: Vec<u16> = (0..MEDIUM).map(|_| rng.gen_range(1, 256) as u16).collect();
buf[MEDIUM - 1] = 0;
let mut group = c.benchmark_group("medium slice [null at the end of slice]");
group.bench_function("iter::position", |b| {
b.iter(|| buf.iter().position(|&c| c == 0))
});
group.bench_function("unrolled_find_u16s", |b| {
b.iter(|| unrolled_find_u16s(0, &buf))
});
group.finish();
}
criterion_group!(
benches,
bench_small_no_null,
bench_small_null_middle,
bench_small_null_end,
bench_medium_no_null,
bench_medium_null_middle,
bench_medium_null_end,
);
criterion_main!(benches);