/
bench.rs
129 lines (112 loc) · 2.95 KB
/
bench.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
122
123
124
125
126
127
128
129
#![feature(test)]
extern crate test;
mod map {
use phf::phf_map;
use std::collections::{BTreeMap, HashMap};
use test::Bencher;
macro_rules! map_and_match {
($map:ident, $f:ident, $($key:expr => $value:expr,)+) => {
static $map: phf::Map<&'static str, usize> = phf_map! {
$($key => $value),+
};
fn $f(key: &str) -> Option<usize> {
match key {
$($key => Some($value),)+
_ => None
}
}
}
}
map_and_match! { MAP, match_get,
"apple" => 0,
"banana" => 1,
"carrot" => 2,
"doughnut" => 3,
"eggplant" => 4,
"frankincene" => 5,
"grapes" => 6,
"haggis" => 7,
"ice cream" => 8,
"jelly beans" => 9,
"kaffir lime leaves" => 10,
"lemonade" => 11,
"mashmallows" => 12,
"nectarines" => 13,
"oranges" => 14,
"pineapples" => 15,
"quinoa" => 16,
"rosemary" => 17,
"sourdough" => 18,
"tomatoes" => 19,
"unleavened bread" => 20,
"vanilla" => 21,
"watermelon" => 22,
"xinomavro grapes" => 23,
"yogurt" => 24,
"zucchini" => 25,
}
#[bench]
fn bench_match_some(b: &mut Bencher) {
b.iter(|| {
assert_eq!(match_get("zucchini").unwrap(), 25);
})
}
#[bench]
fn bench_match_none(b: &mut Bencher) {
b.iter(|| {
assert_eq!(match_get("potato"), None);
})
}
#[bench]
fn bench_btreemap_some(b: &mut Bencher) {
let mut map = BTreeMap::new();
for (key, value) in MAP.entries() {
map.insert(*key, *value);
}
b.iter(|| {
assert_eq!(map.get("zucchini").unwrap(), &25);
})
}
#[bench]
fn bench_hashmap_some(b: &mut Bencher) {
let mut map = HashMap::new();
for (key, value) in MAP.entries() {
map.insert(*key, *value);
}
b.iter(|| {
assert_eq!(map.get("zucchini").unwrap(), &25);
})
}
#[bench]
fn bench_phf_some(b: &mut Bencher) {
b.iter(|| {
assert_eq!(MAP.get("zucchini").unwrap(), &25);
})
}
#[bench]
fn bench_btreemap_none(b: &mut Bencher) {
let mut map = BTreeMap::new();
for (key, value) in MAP.entries() {
map.insert(*key, *value);
}
b.iter(|| {
assert_eq!(map.get("potato"), None);
})
}
#[bench]
fn bench_hashmap_none(b: &mut Bencher) {
let mut map = HashMap::new();
for (key, value) in MAP.entries() {
map.insert(*key, *value);
}
b.iter(|| {
assert_eq!(map.get("potato"), None);
})
}
#[bench]
fn bench_phf_none(b: &mut Bencher) {
b.iter(|| {
assert_eq!(MAP.get("potato"), None);
})
}
}