Skip to content

Instantly share code, notes, and snippets.

@bluss
Last active October 20, 2016 13:16
Show Gist options
  • Save bluss/baa98105d141cff3949dda1c1f2d8cce to your computer and use it in GitHub Desktop.
Save bluss/baa98105d141cff3949dda1c1f2d8cce to your computer and use it in GitHub Desktop.
Using rustc -Copt-level=3 --test extend.rs && ./extend --bench
name before-extend.log ns/iter after-extend.log ns/iter diff ns/iter diff %
bench_chain_collect 53,317 18,039 -35,278 -66.17%
bench_chain_extend_ref 18,105 17,945 -160 -0.88%
bench_chain_extend_value 22,425 18,016 -4,409 -19.66%
bench_map_fast 5,970 6,021 51 0.85%
bench_map_regular 14,242 6,088 -8,154 -57.25%
bench_range_map_collect 12,727 1,954 -10,773 -84.65%
bench_rev_1 14,132 2,574 -11,558 -81.79%
bench_rev_2 14,138 2,620 -11,518 -81.47%
test bench_chain_collect ... bench: 18,039 ns/iter (+/- 215)
test bench_chain_extend_ref ... bench: 17,945 ns/iter (+/- 677)
test bench_chain_extend_value ... bench: 18,016 ns/iter (+/- 707)
test bench_map_fast ... bench: 6,021 ns/iter (+/- 85)
test bench_map_regular ... bench: 6,088 ns/iter (+/- 317)
test bench_range_map_collect ... bench: 1,954 ns/iter (+/- 60)
test bench_rev_1 ... bench: 2,574 ns/iter (+/- 96)
test bench_rev_2 ... bench: 2,620 ns/iter (+/- 99)
test bench_chain_collect ... bench: 53,317 ns/iter (+/- 388)
test bench_chain_extend_ref ... bench: 18,105 ns/iter (+/- 657)
test bench_chain_extend_value ... bench: 22,425 ns/iter (+/- 687)
test bench_map_fast ... bench: 5,970 ns/iter (+/- 219)
test bench_map_regular ... bench: 14,242 ns/iter (+/- 425)
test bench_range_map_collect ... bench: 12,727 ns/iter (+/- 467)
test bench_rev_1 ... bench: 14,132 ns/iter (+/- 444)
test bench_rev_2 ... bench: 14,138 ns/iter (+/- 500)
#![crate_type="rlib"]
#![feature(test)]
extern crate test;
use test::black_box;
pub fn example_plain_slow(l: &[u32]) -> Vec<u32> {
let mut result = Vec::with_capacity(l.len());
result.extend(l.iter().rev());
result
}
pub fn map_fast(l: &[(u32, u32)]) -> Vec<u32> {
let mut result = Vec::with_capacity(l.len());
for i in 0..l.len() {
unsafe {
*result.get_unchecked_mut(i) = l[i].0;
result.set_len(i);
}
}
result
}
const LEN: usize = 16384;
#[bench]
fn bench_range_map_collect(b: &mut test::Bencher) {
b.iter(|| {
(0..LEN).map(|_| u32::default()).collect::<Vec<_>>()
});
}
#[bench]
fn bench_chain_collect(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
data.iter().cloned().chain([1].iter().cloned()).collect::<Vec<_>>()
});
}
#[bench]
fn bench_chain_extend_ref(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
let mut v = Vec::<u32>::with_capacity(data.len() + 1);
v.extend(data.iter().chain([1].iter()));
v
});
}
#[bench]
fn bench_chain_extend_value(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
let mut v = Vec::<u32>::with_capacity(data.len() + 1);
v.extend(data.iter().cloned().chain(Some(1)));
v
});
}
#[bench]
fn bench_rev_1(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
let mut v = Vec::<u32>::new();
v.extend(data.iter().rev());
v
});
}
#[bench]
fn bench_rev_2(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
example_plain_slow(&data);
});
}
#[bench]
fn bench_map_regular(b: &mut test::Bencher) {
let data = black_box([(0, 0); LEN]);
b.iter(|| {
let mut v = Vec::<u32>::new();
v.extend(data.iter().map(|t| t.1));
v
});
}
#[bench]
fn bench_map_fast(b: &mut test::Bencher) {
let data = black_box([(0, 0); LEN]);
b.iter(|| {
map_fast(&data)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment