1 | use criterion::{criterion_group, criterion_main, Criterion}; |
2 | use itertools::Itertools; |
3 | |
4 | struct Unspecialized<I>(I); |
5 | |
6 | impl<I> Iterator for Unspecialized<I> |
7 | where I: Iterator |
8 | { |
9 | type Item = I::Item; |
10 | |
11 | #[inline (always)] |
12 | fn next(&mut self) -> Option<Self::Item> { |
13 | self.0.next() |
14 | } |
15 | |
16 | #[inline (always)] |
17 | fn size_hint(&self) -> (usize, Option<usize>) { |
18 | self.0.size_hint() |
19 | } |
20 | } |
21 | |
22 | mod specialization { |
23 | use super::*; |
24 | |
25 | pub mod intersperse { |
26 | use super::*; |
27 | |
28 | pub fn external(c: &mut Criterion) |
29 | { |
30 | let arr = [1; 1024]; |
31 | |
32 | c.bench_function("external" , move |b| { |
33 | b.iter(|| { |
34 | let mut sum = 0; |
35 | for &x in arr.iter().intersperse(&0) { |
36 | sum += x; |
37 | } |
38 | sum |
39 | }) |
40 | }); |
41 | } |
42 | |
43 | pub fn internal_specialized(c: &mut Criterion) |
44 | { |
45 | let arr = [1; 1024]; |
46 | |
47 | c.bench_function("internal specialized" , move |b| { |
48 | b.iter(|| { |
49 | arr.iter().intersperse(&0).fold(0, |acc, x| acc + x) |
50 | }) |
51 | }); |
52 | } |
53 | |
54 | pub fn internal_unspecialized(c: &mut Criterion) |
55 | { |
56 | let arr = [1; 1024]; |
57 | |
58 | c.bench_function("internal unspecialized" , move |b| { |
59 | b.iter(|| { |
60 | Unspecialized(arr.iter().intersperse(&0)).fold(0, |acc, x| acc + x) |
61 | }) |
62 | }); |
63 | } |
64 | } |
65 | } |
66 | |
67 | criterion_group!( |
68 | benches, |
69 | specialization::intersperse::external, |
70 | specialization::intersperse::internal_specialized, |
71 | specialization::intersperse::internal_unspecialized, |
72 | ); |
73 | criterion_main!(benches); |
74 | |