3 use core::num::Wrapping;
4 use test::{black_box, Bencher};
7 fn bench_rposition(b: &mut Bencher) {
8 let it: Vec<usize> = (0..300).collect();
10 it.iter().rposition(|&x| x <= 150);
15 fn bench_skip_while(b: &mut Bencher) {
28 fn bench_multiple_take(b: &mut Bencher) {
29 let mut it = (0..42).cycle();
31 let n = it.next().unwrap();
33 it.clone().take(it.next().unwrap()).all(|_| true);
38 fn scatter(x: i32) -> i32 {
43 fn bench_max_by_key(b: &mut Bencher) {
46 it.map(black_box).max_by_key(|&x| scatter(x))
50 // https://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
52 fn bench_max_by_key2(b: &mut Bencher) {
53 fn max_index_iter(array: &[i32]) -> usize {
54 array.iter().enumerate().max_by_key(|&(_, item)| item).unwrap().0
57 let mut data = vec![0; 1638];
60 b.iter(|| max_index_iter(&data));
64 fn bench_max(b: &mut Bencher) {
67 it.map(black_box).map(scatter).max()
71 pub fn copy_zip(xs: &[u8], ys: &mut [u8]) {
72 for (a, b) in ys.iter_mut().zip(xs) {
77 pub fn add_zip(xs: &[f32], ys: &mut [f32]) {
78 for (a, b) in ys.iter_mut().zip(xs) {
84 fn bench_zip_copy(b: &mut Bencher) {
85 let source = vec![0u8; 16 * 1024];
86 let mut dst = black_box(vec![0u8; 16 * 1024]);
87 b.iter(|| copy_zip(&source, &mut dst))
91 fn bench_zip_add(b: &mut Bencher) {
92 let source = vec![1.; 16 * 1024];
93 let mut dst = vec![0.; 16 * 1024];
94 b.iter(|| add_zip(&source, &mut dst));
97 /// `Iterator::for_each` implemented as a plain loop.
98 fn for_each_loop<I, F>(iter: I, mut f: F)
108 /// `Iterator::for_each` implemented with `fold` for internal iteration.
109 /// (except when `by_ref()` effectively disables that optimization.)
110 fn for_each_fold<I, F>(iter: I, mut f: F)
115 iter.fold((), move |(), item| f(item));
119 fn bench_for_each_chain_loop(b: &mut Bencher) {
122 let iter = (0i64..1000000).chain(0..1000000).map(black_box);
123 for_each_loop(iter, |x| acc += x);
129 fn bench_for_each_chain_fold(b: &mut Bencher) {
132 let iter = (0i64..1000000).chain(0..1000000).map(black_box);
133 for_each_fold(iter, |x| acc += x);
139 fn bench_for_each_chain_ref_fold(b: &mut Bencher) {
142 let mut iter = (0i64..1000000).chain(0..1000000).map(black_box);
143 for_each_fold(iter.by_ref(), |x| acc += x);
148 /// Helper to benchmark `sum` for iterators taken by value which
149 /// can optimize `fold`, and by reference which cannot.
150 macro_rules! bench_sums {
151 ($bench_sum:ident, $bench_ref_sum:ident, $iter:expr) => {
153 fn $bench_sum(b: &mut Bencher) {
154 b.iter(|| -> i64 { $iter.map(black_box).sum() });
158 fn $bench_ref_sum(b: &mut Bencher) {
159 b.iter(|| -> i64 { $iter.map(black_box).by_ref().sum() });
166 bench_flat_map_ref_sum,
167 (0i64..1000).flat_map(|x| x..x+1000)
171 bench_flat_map_chain_sum,
172 bench_flat_map_chain_ref_sum,
173 (0i64..1000000).flat_map(|x| once(x).chain(once(x)))
178 bench_enumerate_ref_sum,
179 (0i64..1000000).enumerate().map(|(i, x)| x * i as i64)
183 bench_enumerate_chain_sum,
184 bench_enumerate_chain_ref_sum,
185 (0i64..1000000).chain(0..1000000).enumerate().map(|(i, x)| x * i as i64)
190 bench_filter_ref_sum,
191 (0i64..1000000).filter(|x| x % 3 == 0)
195 bench_filter_chain_sum,
196 bench_filter_chain_ref_sum,
197 (0i64..1000000).chain(0..1000000).filter(|x| x % 3 == 0)
201 bench_filter_map_sum,
202 bench_filter_map_ref_sum,
203 (0i64..1000000).filter_map(|x| x.checked_mul(x))
207 bench_filter_map_chain_sum,
208 bench_filter_map_chain_ref_sum,
209 (0i64..1000000).chain(0..1000000).filter_map(|x| x.checked_mul(x))
215 (0i64..1000000).fuse()
219 bench_fuse_chain_sum,
220 bench_fuse_chain_ref_sum,
221 (0i64..1000000).chain(0..1000000).fuse()
226 bench_inspect_ref_sum,
227 (0i64..1000000).inspect(|_| {})
231 bench_inspect_chain_sum,
232 bench_inspect_chain_ref_sum,
233 (0i64..1000000).chain(0..1000000).inspect(|_| {})
238 bench_peekable_ref_sum,
239 (0i64..1000000).peekable()
243 bench_peekable_chain_sum,
244 bench_peekable_chain_ref_sum,
245 (0i64..1000000).chain(0..1000000).peekable()
251 (0i64..1000000).skip(1000)
255 bench_skip_chain_sum,
256 bench_skip_chain_ref_sum,
257 (0i64..1000000).chain(0..1000000).skip(1000)
261 bench_skip_while_sum,
262 bench_skip_while_ref_sum,
263 (0i64..1000000).skip_while(|&x| x < 1000)
267 bench_skip_while_chain_sum,
268 bench_skip_while_chain_ref_sum,
269 (0i64..1000000).chain(0..1000000).skip_while(|&x| x < 1000)
273 bench_take_while_chain_sum,
274 bench_take_while_chain_ref_sum,
275 (0i64..1000000).chain(1000000..).take_while(|&x| x < 1111111)
279 bench_cycle_take_sum,
280 bench_cycle_take_ref_sum,
281 (0..10000).cycle().take(1000000)
285 bench_cycle_skip_take_sum,
286 bench_cycle_skip_take_ref_sum,
287 (0..100000).cycle().skip(1000000).take(1000000)
291 bench_cycle_take_skip_sum,
292 bench_cycle_take_skip_ref_sum,
293 (0..100000).cycle().take(1000000).skip(100000)
297 bench_skip_cycle_skip_zip_add_sum,
298 bench_skip_cycle_skip_zip_add_ref_sum,
299 (0..100000).skip(100).cycle().skip(100)
300 .zip((0..100000).cycle().skip(10))
306 // Checks whether Skip<Zip<A,B>> is as fast as Zip<Skip<A>, Skip<B>>, from
307 // https://users.rust-lang.org/t/performance-difference-between-iterator-zip-and-skip-order/15743
309 fn bench_zip_then_skip(b: &mut Bencher) {
310 let v: Vec<_> = (0..100_000).collect();
311 let t: Vec<_> = (0..100_000).collect();
318 .take_while(|t| *t.0 < 10100)
319 .map(|(a, b)| *a + *b)
321 assert_eq!(s, 2009900);
325 fn bench_skip_then_zip(b: &mut Bencher) {
326 let v: Vec<_> = (0..100_000).collect();
327 let t: Vec<_> = (0..100_000).collect();
333 .zip(t.iter().skip(10000))
334 .take_while(|t| *t.0 < 10100)
335 .map(|(a, b)| *a + *b)
337 assert_eq!(s, 2009900);
342 fn bench_filter_count(b: &mut Bencher) {
343 b.iter(|| (0i64..1000000).map(black_box).filter(|x| x % 3 == 0).count())
347 fn bench_filter_ref_count(b: &mut Bencher) {
348 b.iter(|| (0i64..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count())
352 fn bench_filter_chain_count(b: &mut Bencher) {
353 b.iter(|| (0i64..1000000).chain(0..1000000).map(black_box).filter(|x| x % 3 == 0).count())
357 fn bench_filter_chain_ref_count(b: &mut Bencher) {
359 (0i64..1000000).chain(0..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count()
364 fn bench_partial_cmp(b: &mut Bencher) {
365 b.iter(|| (0..100000).map(black_box).partial_cmp((0..100000).map(black_box)))
369 fn bench_chain_partial_cmp(b: &mut Bencher) {
371 (0..50000).chain(50000..100000).map(black_box).partial_cmp((0..100000).map(black_box))
376 fn bench_lt(b: &mut Bencher) {
377 b.iter(|| (0..100000).map(black_box).lt((0..100000).map(black_box)))
381 fn bench_trusted_random_access_adapters(b: &mut Bencher) {
382 let vec1: Vec<_> = (0usize..100000).collect();
383 let vec2 = black_box(vec1.clone());
389 .map(|(idx, e)| idx.wrapping_add(e))
390 .zip(vec2.iter().copied())
391 .map(|(a, b)| a.wrapping_add(b))
393 let mut acc: usize = 0;
394 let size = iter.size();
396 // SAFETY: TRA requirements are satisfied by 0..size iteration and then dropping the
398 acc = acc.wrapping_add(unsafe { iter.__iterator_get_unchecked(i) });
404 /// Exercises the iter::Copied specialization for slice::Iter
406 fn bench_copied_array_chunks(b: &mut Bencher) {
407 let v = vec![1u8; 1024];
413 .array_chunks::<{ mem::size_of::<u64>() }>()
415 let d = u64::from_ne_bytes(ary);
416 Wrapping(d.rotate_left(7).wrapping_add(1))
418 .sum::<Wrapping<u64>>()