-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
collections.rs
123 lines (108 loc) · 2.57 KB
/
collections.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
//! Run with:
//!
//! ```sh
//! cargo bench -q -p examples --bench collections
//! ```
use divan::{black_box, AllocProfiler, Bencher};
use std::collections::{BTreeSet, BinaryHeap, HashSet, LinkedList, VecDeque};
pub fn collect_nums<T: FromIterator<i32>>(n: usize) -> T {
black_box(0..(n as i32)).collect()
}
pub trait WithCapacity {
fn with_capacity(c: usize) -> Self;
}
pub trait Clear {
fn clear(&mut self);
}
macro_rules! impl_with_capacity {
($($t:ident),+) => {
$(impl WithCapacity for $t<i32> {
fn with_capacity(c: usize) -> Self {
$t::with_capacity(c)
}
})+
};
}
macro_rules! impl_clear {
($($t:ident),+) => {
$(impl Clear for $t<i32> {
fn clear(&mut self) {
$t::clear(self);
}
})+
};
}
impl_with_capacity!(Vec, VecDeque, BinaryHeap, HashSet);
impl_clear!(Vec, VecDeque, BinaryHeap, HashSet, LinkedList, BTreeSet);
#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();
fn main() {
divan::main();
}
const LENS: &[usize] = &[0, 8, 64, 1024];
#[divan::bench(types = [
Vec<i32>,
VecDeque<i32>,
LinkedList<i32>,
BinaryHeap<i32>,
HashSet<i32>,
BTreeSet<i32>,
])]
fn default<T: Default>() -> T {
T::default()
}
#[divan::bench(
types = [
Vec<i32>,
VecDeque<i32>,
BinaryHeap<i32>,
HashSet<i32>,
],
args = LENS,
)]
fn with_capacity<T: WithCapacity>(bencher: Bencher, len: usize) {
bencher.counter(len).bench(|| T::with_capacity(len))
}
#[divan::bench(
types = [
Vec<i32>,
VecDeque<i32>,
LinkedList<i32>,
BinaryHeap<i32>,
HashSet<i32>,
BTreeSet<i32>,
],
args = LENS,
)]
fn from_iter<T: FromIterator<i32>>(bencher: Bencher, len: usize) {
bencher.counter(len).bench(|| collect_nums::<T>(len))
}
#[divan::bench(
types = [
Vec<i32>,
VecDeque<i32>,
LinkedList<i32>,
BinaryHeap<i32>,
HashSet<i32>,
BTreeSet<i32>,
],
args = LENS,
)]
fn drop<T: FromIterator<i32>>(bencher: Bencher, len: usize) {
bencher.counter(len).with_inputs(|| collect_nums::<T>(len)).bench_values(std::mem::drop);
}
#[divan::bench(
types = [
Vec<i32>,
VecDeque<i32>,
LinkedList<i32>,
BinaryHeap<i32>,
HashSet<i32>,
BTreeSet<i32>,
],
args = LENS,
max_time = 1,
)]
fn clear<T: FromIterator<i32> + Clear>(bencher: Bencher, len: usize) {
bencher.counter(len).with_inputs(|| collect_nums::<T>(len)).bench_refs(T::clear);
}