1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use test::{Bencher, black_box};
15 fn bench_rposition(b: &mut Bencher) {
16 let it: Vec<usize> = (0..300).collect();
18 it.iter().rposition(|&x| x <= 150);
23 fn bench_skip_while(b: &mut Bencher) {
27 it.skip_while(|&x| { sum += x; sum < 4000 }).all(|_| true);
32 fn bench_multiple_take(b: &mut Bencher) {
33 let mut it = (0..42).cycle();
35 let n = it.next().unwrap();
37 it.clone().take(it.next().unwrap()).all(|_| true);
42 fn scatter(x: i32) -> i32 { (x * 31) % 127 }
45 fn bench_max_by_key(b: &mut Bencher) {
48 it.max_by_key(|&x| scatter(x))
52 // http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
54 fn bench_max_by_key2(b: &mut Bencher) {
55 fn max_index_iter(array: &[i32]) -> usize {
56 array.iter().enumerate().max_by_key(|&(_, item)| item).unwrap().0
59 let mut data = vec![0; 1638];
62 b.iter(|| max_index_iter(&data));
66 fn bench_max(b: &mut Bencher) {
73 pub fn copy_zip(xs: &[u8], ys: &mut [u8]) {
74 for (a, b) in ys.iter_mut().zip(xs) {
79 pub fn add_zip(xs: &[f32], ys: &mut [f32]) {
80 for (a, b) in ys.iter_mut().zip(xs) {
86 fn bench_zip_copy(b: &mut Bencher) {
87 let source = vec![0u8; 16 * 1024];
88 let mut dst = black_box(vec![0u8; 16 * 1024]);
90 copy_zip(&source, &mut dst)
95 fn bench_zip_add(b: &mut Bencher) {
96 let source = vec![1.; 16 * 1024];
97 let mut dst = vec![0.; 16 * 1024];
99 add_zip(&source, &mut dst)
103 /// `Iterator::for_each` implemented as a plain loop.
104 fn for_each_loop<I, F>(iter: I, mut f: F) where
105 I: Iterator, F: FnMut(I::Item)
112 /// `Iterator::for_each` implemented with `fold` for internal iteration.
113 /// (except when `by_ref()` effectively disables that optimization.)
114 fn for_each_fold<I, F>(iter: I, mut f: F) where
115 I: Iterator, F: FnMut(I::Item)
117 iter.fold((), move |(), item| f(item));
121 fn bench_for_each_chain_loop(b: &mut Bencher) {
124 let iter = (0i64..1000000).chain(0..1000000).map(black_box);
125 for_each_loop(iter, |x| acc += x);
131 fn bench_for_each_chain_fold(b: &mut Bencher) {
134 let iter = (0i64..1000000).chain(0..1000000).map(black_box);
135 for_each_fold(iter, |x| acc += x);
141 fn bench_for_each_chain_ref_fold(b: &mut Bencher) {
144 let mut iter = (0i64..1000000).chain(0..1000000).map(black_box);
145 for_each_fold(iter.by_ref(), |x| acc += x);
151 /// Helper to benchmark `sum` for iterators taken by value which
152 /// can optimize `fold`, and by reference which cannot.
153 macro_rules! bench_sums {
154 ($bench_sum:ident, $bench_ref_sum:ident, $iter:expr) => {
156 fn $bench_sum(b: &mut Bencher) {
158 $iter.map(black_box).sum()
163 fn $bench_ref_sum(b: &mut Bencher) {
165 $iter.map(black_box).by_ref().sum()
173 bench_flat_map_ref_sum,
174 (0i64..1000).flat_map(|x| x..x+1000)
178 bench_flat_map_chain_sum,
179 bench_flat_map_chain_ref_sum,
180 (0i64..1000000).flat_map(|x| once(x).chain(once(x)))
185 bench_enumerate_ref_sum,
186 (0i64..1000000).enumerate().map(|(i, x)| x * i as i64)
190 bench_enumerate_chain_sum,
191 bench_enumerate_chain_ref_sum,
192 (0i64..1000000).chain(0..1000000).enumerate().map(|(i, x)| x * i as i64)
197 bench_filter_ref_sum,
198 (0i64..1000000).filter(|x| x % 2 == 0)
202 bench_filter_chain_sum,
203 bench_filter_chain_ref_sum,
204 (0i64..1000000).chain(0..1000000).filter(|x| x % 2 == 0)
208 bench_filter_map_sum,
209 bench_filter_map_ref_sum,
210 (0i64..1000000).filter_map(|x| x.checked_mul(x))
214 bench_filter_map_chain_sum,
215 bench_filter_map_chain_ref_sum,
216 (0i64..1000000).chain(0..1000000).filter_map(|x| x.checked_mul(x))
222 (0i64..1000000).fuse()
226 bench_fuse_chain_sum,
227 bench_fuse_chain_ref_sum,
228 (0i64..1000000).chain(0..1000000).fuse()
233 bench_inspect_ref_sum,
234 (0i64..1000000).inspect(|_| {})
238 bench_inspect_chain_sum,
239 bench_inspect_chain_ref_sum,
240 (0i64..1000000).chain(0..1000000).inspect(|_| {})
245 bench_peekable_ref_sum,
246 (0i64..1000000).peekable()
250 bench_peekable_chain_sum,
251 bench_peekable_chain_ref_sum,
252 (0i64..1000000).chain(0..1000000).peekable()
258 (0i64..1000000).skip(1000)
262 bench_skip_chain_sum,
263 bench_skip_chain_ref_sum,
264 (0i64..1000000).chain(0..1000000).skip(1000)
268 bench_skip_while_sum,
269 bench_skip_while_ref_sum,
270 (0i64..1000000).skip_while(|&x| x < 1000)
274 bench_skip_while_chain_sum,
275 bench_skip_while_chain_ref_sum,
276 (0i64..1000000).chain(0..1000000).skip_while(|&x| x < 1000)
280 bench_take_while_chain_sum,
281 bench_take_while_chain_ref_sum,
282 (0i64..1000000).chain(1000000..).take_while(|&x| x < 1111111)
285 // Checks whether Skip<Zip<A,B>> is as fast as Zip<Skip<A>, Skip<B>>, from
286 // https://users.rust-lang.org/t/performance-difference-between-iterator-zip-and-skip-order/15743
288 fn bench_zip_then_skip(b: &mut Bencher) {
289 let v: Vec<_> = (0..100_000).collect();
290 let t: Vec<_> = (0..100_000).collect();
293 let s = v.iter().zip(t.iter()).skip(10000)
294 .take_while(|t| *t.0 < 10100)
295 .map(|(a, b)| *a + *b)
297 assert_eq!(s, 2009900);
301 fn bench_skip_then_zip(b: &mut Bencher) {
302 let v: Vec<_> = (0..100_000).collect();
303 let t: Vec<_> = (0..100_000).collect();
306 let s = v.iter().skip(10000).zip(t.iter().skip(10000))
307 .take_while(|t| *t.0 < 10100)
308 .map(|(a, b)| *a + *b)
310 assert_eq!(s, 2009900);