summaryrefslogtreecommitdiffstats
path: root/vendor/itertools/benches/fold_specialization.rs
blob: 3a040305b4627cd333517ee4aec25c3fcaa41d28 (plain)
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
#![allow(unstable_name_collisions)]

use criterion::{criterion_group, criterion_main, Criterion};
use itertools::Itertools;

struct Unspecialized<I>(I);

impl<I> Iterator for Unspecialized<I>
where
    I: Iterator,
{
    type Item = I::Item;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

mod specialization {
    use super::*;

    pub mod intersperse {
        use super::*;

        pub fn external(c: &mut Criterion) {
            let arr = [1; 1024];

            c.bench_function("external", move |b| {
                b.iter(|| {
                    let mut sum = 0;
                    for &x in arr.iter().intersperse(&0) {
                        sum += x;
                    }
                    sum
                })
            });
        }

        pub fn internal_specialized(c: &mut Criterion) {
            let arr = [1; 1024];

            c.bench_function("internal specialized", move |b| {
                b.iter(|| arr.iter().intersperse(&0).fold(0, |acc, x| acc + x))
            });
        }

        pub fn internal_unspecialized(c: &mut Criterion) {
            let arr = [1; 1024];

            c.bench_function("internal unspecialized", move |b| {
                b.iter(|| Unspecialized(arr.iter().intersperse(&0)).fold(0, |acc, x| acc + x))
            });
        }
    }
}

criterion_group!(
    benches,
    specialization::intersperse::external,
    specialization::intersperse::internal_specialized,
    specialization::intersperse::internal_unspecialized,
);
criterion_main!(benches);