-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
extern crate itertools; | ||
|
||
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<I::Item> { | ||
self.0.next() | ||
} | ||
|
||
#[inline(always)] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
mod specialization { | ||
use super::*; | ||
|
||
mod intersperse { | ||
use super::*; | ||
|
||
#[bench] | ||
fn external(b: &mut test::Bencher) | ||
{ | ||
let arr = [1; 1024]; | ||
|
||
b.iter(|| { | ||
let mut sum = 0; | ||
for &x in arr.into_iter().intersperse(&0) { | ||
sum += x; | ||
} | ||
sum | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn internal_specialized(b: &mut test::Bencher) | ||
{ | ||
let arr = [1; 1024]; | ||
|
||
b.iter(|| { | ||
arr.into_iter().intersperse(&0).fold(0, |acc, x| acc + x) | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn internal_unspecialized(b: &mut test::Bencher) | ||
{ | ||
let arr = [1; 1024]; | ||
|
||
b.iter(|| { | ||
Unspecialized(arr.into_iter().intersperse(&0)).fold(0, |acc, x| acc + x) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
extern crate itertools; | ||
|
||
use itertools::Itertools; | ||
|
||
#[test] | ||
fn specialization_intersperse() { | ||
let mut iter = (1..2).intersperse(0); | ||
iter.clone().for_each(|x| assert_eq!(Some(x), iter.next())); | ||
|
||
let mut iter = (1..3).intersperse(0); | ||
iter.clone().for_each(|x| assert_eq!(Some(x), iter.next())); | ||
|
||
let mut iter = (1..4).intersperse(0); | ||
iter.clone().for_each(|x| assert_eq!(Some(x), iter.next())); | ||
} |