Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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)] |
Joel Galenson | 6f79871 | 2021-04-01 17:03:06 -0700 | [diff] [blame] | 12 | fn next(&mut self) -> Option<Self::Item> { |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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); |