-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcollect_filter.rs
More file actions
178 lines (150 loc) · 5.28 KB
/
collect_filter.rs
File metadata and controls
178 lines (150 loc) · 5.28 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use orx_parallel::*;
use orx_split_vec::SplitVec;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rayon::iter::IntoParallelIterator;
use std::hint::black_box;
const TEST_LARGE_OUTPUT: bool = false;
const LARGE_OUTPUT_LEN: usize = match TEST_LARGE_OUTPUT {
true => 64,
false => 0,
};
const SEED: u64 = 5426;
const FIB_UPPER_BOUND: u32 = 201;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Output {
name: String,
numbers: [i64; LARGE_OUTPUT_LEN],
}
fn to_output(idx: &usize) -> Output {
let idx = *idx;
let prefix = match idx % 7 {
0 => "zero-",
3 => "three-",
_ => "sth-",
};
let fib = fibonacci(&(idx as u32));
let name = format!("{}-fib-{}", prefix, fib);
let mut numbers = [0i64; LARGE_OUTPUT_LEN];
for (i, x) in numbers.iter_mut().enumerate() {
*x = match (idx * 7 + i) % 3 {
0 => idx as i64 + i as i64,
_ => idx as i64 - i as i64,
};
}
Output { name, numbers }
}
fn filter(output: &&Output) -> bool {
let last_char = output.name.chars().last().unwrap();
let last_digit: u32 = last_char.to_string().parse().unwrap();
last_digit < 4
}
fn fibonacci(n: &u32) -> u32 {
let mut a = 0;
let mut b = 1;
for _ in 0..*n {
let c = a + b;
a = b;
b = c;
}
a
}
fn inputs(len: usize) -> Vec<Output> {
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
(0..len)
.map(|_| rng.random_range(0..FIB_UPPER_BOUND) as usize)
.map(|x| to_output(&x))
.collect()
}
fn seq(inputs: &[Output]) -> Vec<&Output> {
inputs.iter().filter(filter).collect()
}
fn rayon(inputs: &[Output]) -> Vec<&Output> {
use rayon::iter::ParallelIterator;
inputs.into_par_iter().filter(filter).collect()
}
fn orx_into_vec(inputs: &[Output]) -> Vec<&Output> {
inputs.into_par().filter(filter).collect()
}
fn orx_into_split_vec(inputs: &[Output]) -> SplitVec<&Output> {
inputs.into_par().filter(filter).collect()
}
#[allow(dead_code)]
fn orx_into_vec_with<P: ParThreadPool>(inputs: &[Output], pool: P) -> Vec<&Output> {
inputs.into_par().with_pool(pool).filter(filter).collect()
}
fn run(c: &mut Criterion) {
let treatments = [65_536 * 2];
let mut group = c.benchmark_group("collect_filter");
for n in &treatments {
let input = inputs(*n);
let expected = seq(&input);
let mut sorted = seq(&input);
sorted.sort();
group.bench_with_input(BenchmarkId::new("seq-into-vec", n), n, |b, _| {
assert_eq!(&expected, &seq(&input));
b.iter(|| seq(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("rayon-into-vec", n), n, |b, _| {
assert_eq!(&expected, &rayon(&input));
b.iter(|| rayon(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("orx-vec", n), n, |b, _| {
assert_eq!(&expected, &orx_into_vec(&input));
b.iter(|| orx_into_vec(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("orx-split-vec", n), n, |b, _| {
assert_eq!(&expected, &orx_into_split_vec(&input));
b.iter(|| orx_into_split_vec(black_box(&input)))
});
#[cfg(feature = "rayon-core")]
group.bench_with_input(
BenchmarkId::new("orx-vec (rayon-core::ThreadPool)", n),
n,
|b, _| {
let pool = rayon_core::ThreadPoolBuilder::new()
.num_threads(32)
.build()
.unwrap();
assert_eq!(&expected, &orx_into_vec_with(&input, &pool));
b.iter(|| orx_into_vec_with(black_box(&input), &pool))
},
);
#[cfg(feature = "scoped-pool")]
group.bench_with_input(
BenchmarkId::new("orx-vec (scoped-pool::Pool)", n),
n,
|b, _| {
let pool = scoped_pool::Pool::new(32);
assert_eq!(&expected, &orx_into_vec_with(&input, &pool));
b.iter(|| orx_into_vec_with(black_box(&input), &pool))
},
);
#[cfg(feature = "scoped_threadpool")]
group.bench_with_input(
BenchmarkId::new("orx-vec (scoped_threadpool::Pool)", n),
n,
|b, _| {
let pool = || scoped_threadpool::Pool::new(32);
assert_eq!(&expected, &orx_into_vec_with(&input, pool()));
b.iter(|| orx_into_vec_with(black_box(&input), pool()))
},
);
#[cfg(feature = "yastl")]
group.bench_with_input(BenchmarkId::new("orx-vec (yastl::Pool)", n), n, |b, _| {
let pool = YastlPool::new(32);
assert_eq!(&expected, &orx_into_vec_with(&input, &pool));
b.iter(|| orx_into_vec_with(black_box(&input), &pool))
});
#[cfg(feature = "pond")]
group.bench_with_input(BenchmarkId::new("orx-vec (pond::Pool)", n), n, |b, _| {
let pool = || PondPool::new_threads_unbounded(32);
assert_eq!(&expected, &orx_into_vec_with(&input, pool()));
b.iter(|| orx_into_vec_with(black_box(&input), pool()))
});
}
group.finish();
}
criterion_group!(benches, run);
criterion_main!(benches);