acc
});
}
+
+
+/// Helper to benchmark `sum` for iterators taken by value which
+/// can optimize `fold`, and by reference which cannot.
+macro_rules! bench_sums {
+ ($bench_sum:ident, $bench_ref_sum:ident, $iter:expr) => {
+ #[bench]
+ fn $bench_sum(b: &mut Bencher) {
+ b.iter(|| -> i64 {
+ $iter.map(black_box).sum()
+ });
+ }
+
+ #[bench]
+ fn $bench_ref_sum(b: &mut Bencher) {
+ b.iter(|| -> i64 {
+ $iter.map(black_box).by_ref().sum()
+ });
+ }
+ }
+}
+
+bench_sums! {
+ bench_flat_map_sum,
+ bench_flat_map_ref_sum,
+ (0i64..1000).flat_map(|x| x..x+1000)
+}
+
+bench_sums! {
+ bench_flat_map_chain_sum,
+ bench_flat_map_chain_ref_sum,
+ (0i64..1000000).flat_map(|x| once(x).chain(once(x)))
+}
+
+bench_sums! {
+ bench_enumerate_sum,
+ bench_enumerate_ref_sum,
+ (0i64..1000000).enumerate().map(|(i, x)| x * i as i64)
+}
+
+bench_sums! {
+ bench_enumerate_chain_sum,
+ bench_enumerate_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).enumerate().map(|(i, x)| x * i as i64)
+}
+
+bench_sums! {
+ bench_filter_sum,
+ bench_filter_ref_sum,
+ (0i64..1000000).filter(|x| x % 2 == 0)
+}
+
+bench_sums! {
+ bench_filter_chain_sum,
+ bench_filter_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).filter(|x| x % 2 == 0)
+}
+
+bench_sums! {
+ bench_filter_map_sum,
+ bench_filter_map_ref_sum,
+ (0i64..1000000).filter_map(|x| x.checked_mul(x))
+}
+
+bench_sums! {
+ bench_filter_map_chain_sum,
+ bench_filter_map_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).filter_map(|x| x.checked_mul(x))
+}
+
+bench_sums! {
+ bench_fuse_sum,
+ bench_fuse_ref_sum,
+ (0i64..1000000).fuse()
+}
+
+bench_sums! {
+ bench_fuse_chain_sum,
+ bench_fuse_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).fuse()
+}
+
+bench_sums! {
+ bench_inspect_sum,
+ bench_inspect_ref_sum,
+ (0i64..1000000).inspect(|_| {})
+}
+
+bench_sums! {
+ bench_inspect_chain_sum,
+ bench_inspect_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).inspect(|_| {})
+}
+
+bench_sums! {
+ bench_peekable_sum,
+ bench_peekable_ref_sum,
+ (0i64..1000000).peekable()
+}
+
+bench_sums! {
+ bench_peekable_chain_sum,
+ bench_peekable_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).peekable()
+}
+
+bench_sums! {
+ bench_skip_sum,
+ bench_skip_ref_sum,
+ (0i64..1000000).skip(1000)
+}
+
+bench_sums! {
+ bench_skip_chain_sum,
+ bench_skip_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).skip(1000)
+}
+
+bench_sums! {
+ bench_skip_while_sum,
+ bench_skip_while_ref_sum,
+ (0i64..1000000).skip_while(|&x| x < 1000)
+}
+
+bench_sums! {
+ bench_skip_while_chain_sum,
+ bench_skip_while_chain_ref_sum,
+ (0i64..1000000).chain(0..1000000).skip_while(|&x| x < 1000)
+}