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)