Skip to content

Commit

Permalink
TEST: Tweak extend benchmarks, add write benchmark
Browse files Browse the repository at this point in the history
.write() is explicitly memcpy, so it's nice to compare with.
  • Loading branch information
bluss committed Nov 28, 2018
1 parent fa95a80 commit 675182b
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions benches/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
extern crate arrayvec;
#[macro_use] extern crate bencher;

use std::io::Write;

use arrayvec::ArrayVec;

use bencher::Bencher;
Expand All @@ -12,8 +14,9 @@ fn extend_with_constant(b: &mut Bencher) {
let cap = v.capacity();
b.iter(|| {
v.clear();
v.extend((0..cap).map(|_| 1));
v[0]
let constant = black_box(1);
v.extend((0..cap).map(move |_| constant));
v[511]
});
b.bytes = v.capacity() as u64;
}
Expand All @@ -23,8 +26,9 @@ fn extend_with_range(b: &mut Bencher) {
let cap = v.capacity();
b.iter(|| {
v.clear();
v.extend((0..cap).map(|x| x as _));
v[0]
let range = 0..cap;
v.extend(range.map(|x| black_box(x as _)));
v[511]
});
b.bytes = v.capacity() as u64;
}
Expand All @@ -34,8 +38,20 @@ fn extend_with_slice(b: &mut Bencher) {
let data = [1; 512];
b.iter(|| {
v.clear();
v.extend(black_box(data.iter()).cloned());
v[0]
let iter = data.iter().map(|&x| x);
v.extend(iter);
v[511]
});
b.bytes = v.capacity() as u64;
}

fn extend_with_write(b: &mut Bencher) {
let mut v = ArrayVec::<[u8; 512]>::new();
let data = [1; 512];
b.iter(|| {
v.clear();
v.write(&data[..]).ok();
v[511]
});
b.bytes = v.capacity() as u64;
}
Expand All @@ -51,5 +67,12 @@ fn extend_from_slice(b: &mut Bencher) {
b.bytes = v.capacity() as u64;
}

benchmark_group!(benches, extend_with_constant, extend_with_range, extend_with_slice, extend_from_slice);
benchmark_group!(benches,
extend_with_constant,
extend_with_range,
extend_with_slice,
extend_with_write,
extend_from_slice
);

benchmark_main!(benches);

0 comments on commit 675182b

Please sign in to comment.