-
Notifications
You must be signed in to change notification settings - Fork 15
/
patterns.rs
103 lines (87 loc) · 3.84 KB
/
patterns.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
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use glob::Pattern;
use regex::Regex;
use regex_lite::Regex as RegexLite;
use wildmatch::WildMatch;
const TEXT: &str = "Lorem ipsum dolor sit amet, \
consetetur sadipscing elitr, sed diam nonumy eirmod tempor \
invidunt ut labore et dolore magna aliquyam erat, sed diam \
voluptua. At vero eos et accusam et justo duo dolores et ea \
rebum. Stet clita kasd gubergren, no sea takimata sanctus est \
Lorem ipsum dolor sit amet.";
const FULL_TEXT_PATTERN: &str = TEXT;
const FULL_TEXT_REGEX: &str = "^Lorem ipsum dolor sit amet, \
consetetur sadipscing elitr, sed diam nonumy eirmod tempor \
invidunt ut labore et dolore magna aliquyam erat, sed diam \
voluptua\\. At vero eos et accusam et justo duo dolores et ea \
rebum\\. Stet clita kasd gubergren, no sea takimata sanctus est \
Lorem ipsum dolor sit amet\\.$";
const COMPLEX_PATTERN: &str = "Lorem?ipsum*dolore*ea* ?????ata*.";
const COMPLEX_REGEX: &str = "^Lorem.ipsum.*dolore.*ea.* .....ata.*\\.$";
const MOST_COMPLEX_PATTERN: &str = "?a*b*?**c?d****?e*f*g*?*h?i*?*?**j*******k";
const MOST_COMPLEX_REGEX: &str =
"^.a.*b.*..*.*c.d.*.*.*.*.e.*f.*g.*..*h.i.*..*..*.*j.*.*.*.*.*.*.*k$";
pub fn compiling(c: &mut Criterion) {
let mut group = c.benchmark_group("compiling");
group.bench_function("compile text (wildmatch)", |b| {
b.iter(|| WildMatch::new(black_box(FULL_TEXT_PATTERN)))
});
group.bench_function("compile complex (wildmatch)", |b| {
b.iter(|| WildMatch::new(black_box(MOST_COMPLEX_PATTERN)))
});
group.bench_function("compile text (regex)", |b| {
b.iter(|| Regex::new(black_box(FULL_TEXT_REGEX)).unwrap())
});
group.bench_function("compile complex (regex)", |b| {
b.iter(|| Regex::new(black_box(MOST_COMPLEX_REGEX)).unwrap())
});
group.bench_function("compile text (glob)", |b| {
b.iter(|| Pattern::new(black_box(FULL_TEXT_PATTERN)))
});
group.bench_function("compile complex (glob)", |b| {
b.iter(|| Pattern::new(black_box(MOST_COMPLEX_PATTERN)))
});
group.bench_function("compile text (regex_lite)", |b| {
b.iter(|| RegexLite::new(black_box(FULL_TEXT_PATTERN)))
});
group.bench_function("compile complex (regex_lite)", |b| {
b.iter(|| RegexLite::new(black_box(MOST_COMPLEX_PATTERN)))
});
}
pub fn matching(c: &mut Criterion) {
let pattern1 = WildMatch::new(FULL_TEXT_PATTERN);
let pattern2 = WildMatch::new(COMPLEX_PATTERN);
let regex1 = Regex::new(FULL_TEXT_REGEX).unwrap();
let regex2 = Regex::new(COMPLEX_REGEX).unwrap();
let glob1 = Pattern::new(FULL_TEXT_PATTERN).unwrap();
let glob2 = Pattern::new(COMPLEX_PATTERN).unwrap();
let regex_lite1 = RegexLite::new(FULL_TEXT_PATTERN).unwrap();
let regex_lite2 = RegexLite::new(COMPLEX_PATTERN).unwrap();
let mut group = c.benchmark_group("matching");
group.bench_function("match text (wildmatch)", |b| {
b.iter(|| pattern1 == black_box(TEXT))
});
group.bench_function("match complex (wildmatch)", |b| {
b.iter(|| pattern2 == black_box(TEXT))
});
group.bench_function("match text (regex)", |b| {
b.iter(|| regex1.is_match(black_box(TEXT)))
});
group.bench_function("match complex (regex)", |b| {
b.iter(|| regex2.is_match(black_box(TEXT)))
});
group.bench_function("match text (glob)", |b| {
b.iter(|| glob1.matches(black_box(TEXT)))
});
group.bench_function("match complex (glob)", |b| {
b.iter(|| glob2.matches(black_box(TEXT)))
});
group.bench_function("match text (regex_lite)", |b| {
b.iter(|| regex_lite1.is_match(black_box(TEXT)))
});
group.bench_function("match complex (regex_lite)", |b| {
b.iter(|| regex_lite2.is_match(black_box(TEXT)))
});
}
criterion_group!(benches, compiling, matching);
criterion_main!(benches);