-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_mutation_ends.rs
153 lines (138 loc) · 4.29 KB
/
doubly_mutation_ends.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use orx_linked_list::*;
use orx_selfref_col::MemoryPolicy;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
#[derive(Clone, Copy)]
enum Action {
PushBack(u32),
PushFront(u32),
PopBack,
PopFront,
}
fn get_test_data(n: usize) -> Vec<Action> {
let mut rng = ChaCha8Rng::seed_from_u64(56456);
let mut vec: Vec<_> = (0..n)
.map(|_| match rng.gen::<f32>() {
x if x < 0.5 => Action::PushBack(rng.gen_range(0..n) as u32),
_ => Action::PushFront(rng.gen_range(0..n) as u32),
})
.collect();
for _ in 0..2 * n {
let action = match rng.gen::<f32>() {
x if x < 0.25 => Action::PushBack(rng.gen_range(0..n) as u32),
x if x < 0.50 => Action::PushFront(rng.gen_range(0..n) as u32),
x if x < 0.75 => Action::PopBack,
_ => Action::PopFront,
};
vec.push(action)
}
vec
}
// variants
fn doubly_list<M: MemoryPolicy<Doubly<u32>>>(
actions: &[Action],
list: &mut List<Doubly<u32>, M>,
) -> u64 {
let mut sum = 0;
for action in actions {
let x = match action {
Action::PushBack(x) => {
list.push_back(*x);
None
}
Action::PushFront(x) => {
list.push_front(*x);
None
}
Action::PopBack => list.pop_back(),
Action::PopFront => list.pop_front(),
};
if let Some(x) = x {
sum += x as u64;
}
}
sum
}
fn std_linked_list(actions: &[Action], list: &mut std::collections::LinkedList<u32>) -> u64 {
let mut sum = 0;
for action in actions {
let x = match action {
Action::PushBack(x) => {
list.push_back(*x);
None
}
Action::PushFront(x) => {
list.push_front(*x);
None
}
Action::PopBack => list.pop_back(),
Action::PopFront => list.pop_front(),
};
if let Some(x) = x {
sum += x as u64;
}
}
sum
}
fn std_vec_deque(actions: &[Action], list: &mut std::collections::VecDeque<u32>) -> u64 {
let mut sum = 0;
for action in actions {
let x = match action {
Action::PushBack(x) => {
list.push_back(*x);
None
}
Action::PushFront(x) => {
list.push_front(*x);
None
}
Action::PopBack => list.pop_back(),
Action::PopFront => list.pop_front(),
};
if let Some(x) = x {
sum += x as u64;
}
}
sum
}
fn bench(c: &mut Criterion) {
let treatments = vec![1_024 * 64];
let mut group = c.benchmark_group("doubly_mutation_ends");
for n in &treatments {
let data = get_test_data(*n);
let expected = std_linked_list(&data, &mut std::collections::LinkedList::new());
group.bench_with_input(BenchmarkId::new("LinkedList", n), n, |b, _| {
b.iter(|| {
let mut list = std::collections::LinkedList::new();
let result = std_linked_list(&data, &mut list);
assert_eq!(result, expected);
})
});
group.bench_with_input(BenchmarkId::new("VecDeque", n), n, |b, _| {
b.iter(|| {
let mut list = std::collections::VecDeque::new();
let result = std_vec_deque(&data, &mut list);
assert_eq!(result, expected);
})
});
group.bench_with_input(BenchmarkId::new("DoublyList", n), n, |b, _| {
b.iter(|| {
let mut list = DoublyList::new();
let result = doubly_list(&data, &mut list);
assert_eq!(result, expected);
})
});
group.bench_with_input(BenchmarkId::new("DoublyListLazy", n), n, |b, _| {
b.iter(|| {
let mut list = DoublyListLazy::new();
let result = doubly_list(&data, &mut list);
list.reclaim_closed_nodes();
assert_eq!(result, expected);
})
});
}
group.finish();
}
criterion_group!(benches, bench);
criterion_main!(benches);