|
| 1 | +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
| 2 | +use orx_parallel::*; |
| 3 | +use orx_split_vec::*; |
| 4 | +use rand::prelude::*; |
| 5 | +use rand_chacha::ChaCha8Rng; |
| 6 | +use std::hint::black_box; |
| 7 | + |
| 8 | +const TEST_LARGE_OUTPUT: bool = false; |
| 9 | + |
| 10 | +const LARGE_OUTPUT_LEN: usize = match TEST_LARGE_OUTPUT { |
| 11 | + true => 64, |
| 12 | + false => 0, |
| 13 | +}; |
| 14 | +const SEED: u64 = 5426; |
| 15 | +const FIB_UPPER_BOUND: u32 = 201; |
| 16 | + |
| 17 | +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 18 | +struct Output { |
| 19 | + name: String, |
| 20 | + numbers: [i64; LARGE_OUTPUT_LEN], |
| 21 | +} |
| 22 | + |
| 23 | +fn map(idx: usize) -> Output { |
| 24 | + let prefix = match idx % 7 { |
| 25 | + 0 => "zero-", |
| 26 | + 3 => "three-", |
| 27 | + _ => "sth-", |
| 28 | + }; |
| 29 | + let fib = fibonacci(&(idx as u32)); |
| 30 | + let name = format!("{}-fib-{}", prefix, fib); |
| 31 | + |
| 32 | + let mut numbers = [0i64; LARGE_OUTPUT_LEN]; |
| 33 | + for (i, x) in numbers.iter_mut().enumerate() { |
| 34 | + *x = match (idx * 7 + i) % 3 { |
| 35 | + 0 => idx as i64 + i as i64, |
| 36 | + _ => idx as i64 - i as i64, |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + Output { name, numbers } |
| 41 | +} |
| 42 | + |
| 43 | +fn filter(output: &Output) -> bool { |
| 44 | + let last_char = output.name.chars().last().unwrap(); |
| 45 | + let last_digit: u32 = last_char.to_string().parse().unwrap(); |
| 46 | + last_digit < 4 |
| 47 | +} |
| 48 | + |
| 49 | +fn fibonacci(n: &u32) -> u32 { |
| 50 | + let mut a = 0; |
| 51 | + let mut b = 1; |
| 52 | + for _ in 0..*n { |
| 53 | + let c = a + b; |
| 54 | + a = b; |
| 55 | + b = c; |
| 56 | + } |
| 57 | + a |
| 58 | +} |
| 59 | + |
| 60 | +fn input(len: usize) -> impl Iterator<Item = usize> { |
| 61 | + let mut rng = ChaCha8Rng::seed_from_u64(SEED); |
| 62 | + (0..len).map(move |_| rng.random_range(0..FIB_UPPER_BOUND) as usize) |
| 63 | +} |
| 64 | + |
| 65 | +fn seq(inputs: Vec<usize>) -> Vec<Output> { |
| 66 | + inputs.into_iter().map(map).filter(filter).collect() |
| 67 | +} |
| 68 | + |
| 69 | +fn par_over_vec(inputs: Vec<usize>) -> Vec<Output> { |
| 70 | + inputs.into_par().map(map).filter(filter).collect() |
| 71 | +} |
| 72 | + |
| 73 | +fn par_over_split_vec<G: ParGrowth>(inputs: SplitVec<usize, G>) -> Vec<Output> { |
| 74 | + inputs.into_par().map(map).filter(filter).collect() |
| 75 | +} |
| 76 | + |
| 77 | +fn run(c: &mut Criterion) { |
| 78 | + let treatments = [65_536, 65_536 * 4]; |
| 79 | + |
| 80 | + #[allow(unused_mut)] |
| 81 | + let mut group = c.benchmark_group("par_collect_map_filter_owned"); |
| 82 | + |
| 83 | + for n in &treatments { |
| 84 | + let expected = seq(input(*n).collect()); |
| 85 | + |
| 86 | + let input_doubling = || input(*n).collect::<SplitVec<_, Doubling>>(); |
| 87 | + |
| 88 | + let input_recursive = || input(*n).collect::<SplitVec<_, Recursive>>(); |
| 89 | + |
| 90 | + let input_linear = || { |
| 91 | + let mut input_linear = SplitVec::with_linear_growth(10); |
| 92 | + input_linear.extend(input(*n)); |
| 93 | + input_linear |
| 94 | + }; |
| 95 | + |
| 96 | + group.bench_with_input(BenchmarkId::new("seq", n), n, |b, _| { |
| 97 | + assert_eq!(&expected, &seq(input(*n).collect())); |
| 98 | + b.iter(|| seq(black_box(input(*n).collect()))) |
| 99 | + }); |
| 100 | + |
| 101 | + group.bench_with_input(BenchmarkId::new("par_over_vec", n), n, |b, _| { |
| 102 | + assert_eq!(&expected, &par_over_vec(input(*n).collect())); |
| 103 | + b.iter(|| par_over_vec(black_box(input(*n).collect()))) |
| 104 | + }); |
| 105 | + |
| 106 | + group.bench_with_input( |
| 107 | + BenchmarkId::new("par_over_split_vec_doubling", n), |
| 108 | + n, |
| 109 | + |b, _| { |
| 110 | + assert_eq!(&expected, &par_over_split_vec(input_doubling())); |
| 111 | + b.iter(|| par_over_split_vec(black_box(input_doubling()))) |
| 112 | + }, |
| 113 | + ); |
| 114 | + |
| 115 | + group.bench_with_input( |
| 116 | + BenchmarkId::new("par_over_split_vec_linear", n), |
| 117 | + n, |
| 118 | + |b, _| { |
| 119 | + assert_eq!(&expected, &par_over_split_vec(input_linear())); |
| 120 | + b.iter(|| par_over_split_vec(black_box(input_linear()))) |
| 121 | + }, |
| 122 | + ); |
| 123 | + |
| 124 | + group.bench_with_input( |
| 125 | + BenchmarkId::new("par_over_split_vec_recursive", n), |
| 126 | + n, |
| 127 | + |b, _| { |
| 128 | + assert_eq!(&expected, &par_over_split_vec(input_recursive())); |
| 129 | + b.iter(|| par_over_split_vec(black_box(input_recursive()))) |
| 130 | + }, |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + group.finish(); |
| 135 | +} |
| 136 | + |
| 137 | +criterion_group!(benches, run); |
| 138 | +criterion_main!(benches); |
0 commit comments