Skip to content
Permalink
Browse files

Add benchmarks for Vec to compare to SmallVec

  • Loading branch information...
Vurich committed May 17, 2018
1 parent 8c9f6c8 commit 291eb90ed1eaabf0e02987de9c3c76d1b85b6a53
Showing with 96 additions and 0 deletions.
  1. +96 −0 benches/bench.rs
@@ -130,3 +130,99 @@ fn bench_macro_from_list(b: &mut Bencher) {
vec
});
}
#[bench]
fn bench_push_vec(b: &mut Bencher) {
#[inline(never)]
fn push_noinline(vec: &mut Vec<u64>, x: u64) {
vec.push(x)
}

b.iter(|| {
let mut vec: Vec<u64> = Vec::with_capacity(16);
for x in 0..100 {
push_noinline(&mut vec, x);
}
vec
});
}

#[bench]
fn bench_insert_vec(b: &mut Bencher) {
#[inline(never)]
fn insert_noinline(vec: &mut Vec<u64>, x: u64) {
vec.insert(0, x)
}

b.iter(|| {
let mut vec: Vec<u64> = Vec::with_capacity(16);
for x in 0..100 {
insert_noinline(&mut vec, x);
}
vec
});
}

#[bench]
fn bench_extend_vec(b: &mut Bencher) {
b.iter(|| {
let mut vec: Vec<u64> = Vec::with_capacity(16);
vec.extend(0..100);
vec
});
}

#[bench]
fn bench_from_slice_vec(b: &mut Bencher) {
let v: Vec<u64> = (0..100).collect();
b.iter(|| {
let vec: Vec<u64> = Vec::from(&v[..]);
vec
});
}

#[bench]
fn bench_extend_from_slice_vec(b: &mut Bencher) {
let v: Vec<u64> = (0..100).collect();
b.iter(|| {
let mut vec: Vec<u64> = Vec::with_capacity(16);
vec.extend_from_slice(&v);
vec
});
}

#[bench]
fn bench_pushpop_vec(b: &mut Bencher) {
#[inline(never)]
fn pushpop_noinline(vec: &mut Vec<u64>, x: u64) {
vec.push(x);
vec.pop();
}

b.iter(|| {
let mut vec: Vec<u64> = Vec::with_capacity(16);
for x in 0..100 {
pushpop_noinline(&mut vec, x);
}
vec
});
}

#[bench]
fn bench_macro_from_elem_vec(b: &mut Bencher) {
b.iter(|| {
let vec: Vec<u64> = vec![42; 100];
vec
});
}

#[bench]
fn bench_macro_from_list_vec(b: &mut Bencher) {
b.iter(|| {
let vec: Vec<u64> = vec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000,
0x80000, 0x100000
];
vec
});
}

0 comments on commit 291eb90

Please sign in to comment.
You can’t perform that action at this time.