Skip to content

Commit

Permalink
Auto merge of #68154 - ssomers:btreemap_navigation_benches, r=Mark-Si…
Browse files Browse the repository at this point in the history
…mulacrum

Add more BTreeMap/BTreeSet benchmarks regarding iteration

Serving #67073 or other developments
  • Loading branch information
bors committed Jan 21, 2020
2 parents 06b9450 + 3ada8ae commit 8c73fa7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/liballoc/benches/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ pub fn iter_100000(b: &mut Bencher) {
bench_iter(b, 100000);
}

fn bench_iter_mut(b: &mut Bencher, size: i32) {
let mut map = BTreeMap::<i32, i32>::new();
let mut rng = thread_rng();

for _ in 0..size {
map.insert(rng.gen(), rng.gen());
}

b.iter(|| {
for kv in map.iter_mut() {
black_box(kv);
}
});
}

#[bench]
pub fn iter_mut_20(b: &mut Bencher) {
bench_iter_mut(b, 20);
}

#[bench]
pub fn iter_mut_1000(b: &mut Bencher) {
bench_iter_mut(b, 1000);
}

#[bench]
pub fn iter_mut_100000(b: &mut Bencher) {
bench_iter_mut(b, 100000);
}

fn bench_first_and_last(b: &mut Bencher, size: i32) {
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
b.iter(|| {
Expand Down
47 changes: 39 additions & 8 deletions src/liballoc/benches/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ fn random(n: usize) -> BTreeSet<usize> {
}

fn neg(n: usize) -> BTreeSet<i32> {
let mut set = BTreeSet::new();
for i in -(n as i32)..=-1 {
set.insert(i);
}
let set: BTreeSet<i32> = (-(n as i32)..=-1).collect();
assert_eq!(set.len(), n);
set
}

fn pos(n: usize) -> BTreeSet<i32> {
let mut set = BTreeSet::new();
for i in 1..=(n as i32) {
set.insert(i);
}
let set: BTreeSet<i32> = (1..=(n as i32)).collect();
assert_eq!(set.len(), n);
set
}
Expand Down Expand Up @@ -56,6 +50,43 @@ macro_rules! set_bench {
};
}

const BUILD_SET_SIZE: usize = 100;

#[bench]
pub fn build_and_clear(b: &mut Bencher) {
b.iter(|| pos(BUILD_SET_SIZE).clear())
}

#[bench]
pub fn build_and_drop(b: &mut Bencher) {
b.iter(|| pos(BUILD_SET_SIZE))
}

#[bench]
pub fn build_and_into_iter(b: &mut Bencher) {
b.iter(|| pos(BUILD_SET_SIZE).into_iter().count())
}

#[bench]
pub fn build_and_pop_all(b: &mut Bencher) {
b.iter(|| {
let mut s = pos(BUILD_SET_SIZE);
while s.pop_first().is_some() {}
s
});
}

#[bench]
pub fn build_and_remove_all(b: &mut Bencher) {
b.iter(|| {
let mut s = pos(BUILD_SET_SIZE);
while let Some(elt) = s.iter().copied().next() {
s.remove(&elt);
}
s
});
}

set_bench! {intersection_100_neg_vs_100_pos, intersection, count, [neg(100), pos(100)]}
set_bench! {intersection_100_neg_vs_10k_pos, intersection, count, [neg(100), pos(10_000)]}
set_bench! {intersection_100_pos_vs_100_neg, intersection, count, [pos(100), neg(100)]}
Expand Down

0 comments on commit 8c73fa7

Please sign in to comment.