-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_filter_collect.rs
123 lines (102 loc) · 3.44 KB
/
map_filter_collect.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
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use orx_parallel::prelude::*;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rayon::iter::IntoParallelIterator;
const SEED: u64 = 9874;
const FIB_UPPER_BOUND: u32 = 999;
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 filter(a: &u32) -> bool {
a % 10 == 0 || a % 3 != 1
}
fn inputs(len: usize) -> Vec<u32> {
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
(0..len)
.map(|_| rng.gen_range(0..FIB_UPPER_BOUND))
.collect()
}
fn seq(inputs: &[u32]) -> Vec<u32> {
inputs.iter().map(fibonacci).filter(filter).collect()
}
fn rayon(inputs: &[u32]) -> Vec<u32> {
use rayon::iter::ParallelIterator;
inputs
.into_par_iter()
.map(fibonacci)
.filter(filter)
.collect()
}
fn orx_parallel_split_vec(inputs: &[u32]) -> SplitVec<u32> {
inputs.into_par().map(fibonacci).filter(filter).collect()
}
fn orx_parallel_split_rec(inputs: &[u32]) -> SplitVec<u32, Recursive> {
inputs.into_par().map(fibonacci).filter(filter).collect_x()
}
fn orx_parallel_vec(inputs: &[u32]) -> Vec<u32> {
inputs
.into_par()
.map(fibonacci)
.filter(filter)
.collect_vec()
}
fn orx_parallel(inputs: &[u32], num_threads: usize, chunk_size: usize) -> Vec<u32> {
inputs
.into_par()
.chunk_size(chunk_size)
.num_threads(num_threads)
.map(fibonacci)
.filter(filter)
.collect_vec()
}
fn map_filter_collect(c: &mut Criterion) {
let treatments = vec![65_536, 262_144];
let _params = [(1, 1), (4, 256), (8, 1024), (32, 1024)];
let params = [];
let mut group = c.benchmark_group("map_filter_collect");
for n in &treatments {
let input = inputs(*n);
let expected = seq(&input);
group.bench_with_input(BenchmarkId::new("seq", n), n, |b, _| {
b.iter(|| seq(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("rayon", n), n, |b, _| {
assert_eq!(rayon(&input), expected);
b.iter(|| rayon(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("orx-parallel-split-vec", n), n, |b, _| {
assert_eq!(orx_parallel_split_vec(&input), expected);
b.iter(|| orx_parallel_split_vec(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("orx-parallel-vec", n), n, |b, _| {
assert_eq!(orx_parallel_vec(&input), expected);
b.iter(|| orx_parallel_vec(black_box(&input)))
});
group.bench_with_input(BenchmarkId::new("orx-parallel-split-rec", n), n, |b, _| {
let mut result = orx_parallel_split_rec(&input).to_vec();
result.sort();
let mut expected = expected.clone();
expected.sort();
assert_eq!(result, expected);
b.iter(|| orx_parallel_split_rec(black_box(&input)))
});
for (t, c) in params {
let name = format!("orx-parallel-t{}-c{}", t, c);
group.bench_with_input(BenchmarkId::new(name, n), n, |b, _| {
assert_eq!(orx_parallel(&input, t, c), expected);
b.iter(|| orx_parallel(black_box(&input), t, c))
});
}
}
group.finish();
}
criterion_group!(benches, map_filter_collect);
criterion_main!(benches);