Skip to content

Commit

Permalink
update vec::retain benchmarks
Browse files Browse the repository at this point in the history
Add `into_iter().filter().collect()` as a comparison point since it was reported to be faster than `retain`.
Remove clone inside benchmark loop to reduce allocator noise.
  • Loading branch information
the8472 committed Dec 4, 2021
1 parent 887999d commit d0f38cc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions library/alloc/benches/vec.rs
Expand Up @@ -733,11 +733,26 @@ fn bench_flat_map_collect(b: &mut Bencher) {
b.iter(|| v.iter().flat_map(|color| color.rotate_left(8).to_be_bytes()).collect::<Vec<_>>());
}

/// Reference benchmark that `retain` has to compete with.
#[bench]
fn bench_retain_iter_100000(b: &mut Bencher) {
let mut v = Vec::with_capacity(100000);

b.iter(|| {
let mut tmp = std::mem::take(&mut v);
tmp.clear();
tmp.extend(black_box(1..=100000));
v = tmp.into_iter().filter(|x| x & 1 == 0).collect();
});
}

#[bench]
fn bench_retain_100000(b: &mut Bencher) {
let v = (1..=100000).collect::<Vec<u32>>();
let mut v = Vec::with_capacity(100000);

b.iter(|| {
let mut v = v.clone();
v.clear();
v.extend(black_box(1..=100000));
v.retain(|x| x & 1 == 0)
});
}
Expand Down

0 comments on commit d0f38cc

Please sign in to comment.