Skip to content

Instantly share code, notes, and snippets.

@ArtemGr ArtemGr/bench_iterators.rs
Last active Feb 15, 2016

Embed
What would you like to do?
// [build] rustc -O bench_iterators.rs --test
// [build] ./bench_iterators.exe --bench
#![feature(test)]
extern crate test;
use test::{Bencher, black_box};
#[bench]
fn construction_only(b: &mut Bencher) {
let foo = String::from("foo");
b.iter(|| {
let i1 = foo.chars().flat_map(|c| c.to_uppercase());
black_box(i1);
})
}
#[bench]
fn inplace_construction(b: &mut Bencher) {
let foo = String::from("foo");
b.iter(|| {
let s1: String = foo.chars().flat_map(|c| c.to_uppercase()).collect();
black_box(s1);
let s2: String = foo.chars().flat_map(|c| c.to_uppercase()).collect();
black_box(s2);
})
}
#[bench]
fn closure(b: &mut Bencher) {
let foo = String::from("foo");
let iter = || foo.chars().flat_map(|c| c.to_uppercase());
b.iter(|| {
let s1: String = iter().collect();
black_box(s1);
let s2: String = iter().collect();
black_box(s2);
})
}
#[bench]
fn vec_inplace_iter(b: &mut Bencher) {
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
b.iter(|| {
let i = v.iter().skip(2);
black_box(i);
})
}
#[bench]
fn vec_clone_iter(b: &mut Bencher) {
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
let i = v.iter().skip(2);
b.iter(|| {
let i = i.clone();
black_box(i);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.