Skip to content

Instantly share code, notes, and snippets.

@bluss
Created October 27, 2016 17:20
Show Gist options
  • Save bluss/3828cadbd92f5a94d020a474b9879f6c to your computer and use it in GitHub Desktop.
Save bluss/3828cadbd92f5a94d020a474b9879f6c to your computer and use it in GitHub Desktop.
These benchmarks are a bit noisy and imperfect. We focus on the big changes.
extend-after-1.log is taking advantage of TrustedLen in Vec::extend.
name extend-before-1.log ns/iter extend-after-1.log ns/iter diff ns/iter diff %
bench_chain_all 17,866 17,917 51 0.29%
bench_chain_chain_collect 62,249 32,370 -29,879 -48.00%
bench_chain_collect 26,828 25,386 -1,442 -5.37%
bench_chain_extend_ref 18,070 18,606 536 2.97%
bench_chain_extend_value 23,146 18,101 -5,045 -21.80%
bench_chain_fold 1,417 1,384 -33 -2.33%
bench_collect 228,065 234,066 6,001 2.63%
bench_extend_from_slice 2,578 2,639 61 2.37%
bench_extend_with_vec_1 17 18 1 5.88%
bench_extend_with_vec_2 5,111 5,520 409 8.00%
bench_map_fast 6,061 6,245 184 3.04%
bench_map_regular 14,108 6,429 -7,679 -54.43%
bench_nest_chain_chain_collect 44,562 41,087 -3,475 -7.80%
bench_push_loop 102,615 104,471 1,856 1.81%
bench_range_map_collect 12,794 1,992 -10,802 -84.43%
bench_rev_1 14,143 2,792 -11,351 -80.26%
bench_rev_2 14,089 2,773 -11,316 -80.32%
bench_slice_all 8,893 8,964 71 0.80%
bench_slice_fold 1,383 1,379 -4 -0.29%
#![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_chain_collect(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
data.iter().cloned().chain([1].iter().cloned()).chain([2].iter().cloned()).collect::<Vec<_>>()
});
}
#[bench]
fn bench_nest_chain_chain_collect(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
data.iter().cloned().chain([1].iter().chain([2].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_slice_fold(b: &mut test::Bencher) {
let data = black_box([1; LEN]);
b.iter(|| {
data.iter().fold(0, |x, &y| x + y)
});
}
#[bench]
fn bench_chain_fold(b: &mut test::Bencher) {
let data = black_box([1; LEN]);
b.iter(|| {
data.iter().chain([1].iter()).fold(0, |x, &y| x + y)
});
}
#[bench]
fn bench_slice_all(b: &mut test::Bencher) {
let data = black_box([1; LEN]);
b.iter(|| {
data.iter().all(|&x| x >= 1)
});
}
#[bench]
fn bench_chain_all(b: &mut test::Bencher) {
let data = black_box([1; LEN]);
b.iter(|| {
data.iter().chain([1].iter()).all(|&x| x >= 1)
});
}
#[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_array_16_push_loop(b: &mut test::Bencher) {
b.iter(|| {
let mut v = Vec::new();
v.reserve(LEN);
for _ in 0..LEN {
v.push([0u32; 16]);
}
v
});
}
#[bench]
fn bench_array_16_collect(b: &mut test::Bencher) {
b.iter(|| {
(0..LEN).map(|_| [0u32; 16]).collect::<Vec<_>>()
});
}
#[bench]
fn bench_array_16_vec_macro(b: &mut test::Bencher) {
b.iter(|| {
vec![[0u32; 16]; LEN]
});
}
#[bench]
fn bench_extend_from_slice(b: &mut test::Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
let mut v = Vec::new();
v.extend_from_slice(&data);
v
});
}
#[bench]
fn bench_extend_with_vec_1(b: &mut test::Bencher) {
let mut v = Vec::new();
b.iter(|| {
v.clear();
v.extend(vec![1, 2, 3].into_iter().filter(|_| true));
v[0]
});
}
#[bench]
fn bench_extend_with_vec_2(b: &mut test::Bencher) {
let mut data = vec![0; LEN];
b.iter(|| {
let mut v = Vec::new();
v.extend(data.clone());
v
});
}
#[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