Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

perf: Add benchmarks for BtreeMap::Iterator::count() #191

Merged
merged 8 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions benchmarks/src/btreemap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Bound;

use crate::Random;
use canbench::{benchmark, macros::bench, BenchResult};
use ic_stable_structures::{storable::Blob, BTreeMap, DefaultMemoryImpl, Storable};
Expand Down Expand Up @@ -219,6 +221,53 @@ pub fn btreemap_insert_10mib_values() -> BenchResult {
})
}

#[bench]
pub fn btreemap_iter_count() -> BenchResult {
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved
let mut btree = BTreeMap::new(DefaultMemoryImpl::default());
let size: u8 = 200;
for i in 0..size {
btree.insert(i, i);
}

benchmark(|| {
for i in 0..size {
for j in i + 1..size {
btree
.range((Bound::Included(i), Bound::Included(j)))
.count();
}
}
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved
})
}

#[bench]
pub fn btreemap_iter_count_10mib_values() -> BenchResult {
let mut btree = BTreeMap::new(DefaultMemoryImpl::default());

// Insert 200 10MiB values.
let mut rng = Rng::from_seed(0);
let mut values = vec![];
for _ in 0..200 {
values.push(rng.iter(Rand::rand_u8).take(10 * 1024).collect::<Vec<_>>());
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved
}
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved

let mut i = 0u8;
for value in values.into_iter() {
btree.insert(i, value);
i += 1;
}

benchmark(|| {
for j in 0..i {
for k in j + 1..i {
btree
.range((Bound::Included(j), Bound::Included(k)))
.count();
}
}
})
}

/// Benchmarks removing keys from a BTreeMap.
#[bench]
pub fn btreemap_remove_blob_4_1024() -> BenchResult {
Expand Down
Loading
Loading