use std::collections::HashMap; use criterion::{black_box, Criterion, criterion_group, criterion_main}; use hi_sparse_array::{Empty, SparseArray}; use hi_sparse_array::compact_sparse_array2::CompactSparseArray; use hi_sparse_array::level_block::{Block, ClusterBlock, SmallBlock}; use hi_sparse_array::Iter; use hi_sparse_array::level::{IntrusiveListLevel, SingleBlockLevel}; type Lvl0Block = Block; type Lvl1Block = Block; type Lvl2Block = Block; type CompactLvl1Block = SmallBlock; type ClusterLvl1Block = ClusterBlock; #[derive(Clone, Default)] struct DataBlock(u64); impl Empty for DataBlock{ fn empty() -> Self { Self(0) } fn is_empty(&self) -> bool { todo!() } } type BlockArray = SparseArray<(SingleBlockLevel, IntrusiveListLevel), DataBlock>; type SmallBlockArray = SparseArray<(SingleBlockLevel, IntrusiveListLevel), DataBlock>; type ClusterBlockArray = SparseArray<(SingleBlockLevel, IntrusiveListLevel), DataBlock>; type CompactArray = CompactSparseArray; fn compact_array_iter(array: &CompactArray) -> u64 { use hi_sparse_array::sparse_hierarchy::SparseHierarchy; let mut s = 0; for (_, i) in array.iter(){ s += i.0; } s } fn cluster_array_iter(array: &ClusterBlockArray) -> u64 { let mut s = 0; for (_, i) in Iter::new(array){ s += i.0; } s } fn small_array_iter(array: &SmallBlockArray) -> u64 { let mut s = 0; for (_, i) in Iter::new(array){ s += i.0; } s } fn array_iter(array: &BlockArray) -> u64 { let mut s = 0; for (_, i) in Iter::new(array){ s += i.0; } s } fn vec_iter(array: &Vec) -> u64 { let mut s = 0; for i in array{ s += i.0; } s } fn hashmap_iter(array: &HashMap) -> u64 { let mut s = 0; for (_, i) in array.iter(){ s += i.0; } s } pub fn bench_iter(c: &mut Criterion) { let mut block_array = BlockArray::default(); let mut small_block_array = SmallBlockArray::default(); let mut cluster_block_array = ClusterBlockArray::default(); let mut compact_array = CompactArray::default(); let mut vec = Vec::default(); let mut hashmap = HashMap::default(); for i in 0..3000{ *block_array.get_mut(i) = DataBlock(i as u64); *small_block_array.get_mut(i) = DataBlock(i as u64); *cluster_block_array.get_mut(i) = DataBlock(i as u64); *compact_array.get_or_insert(i) = DataBlock(i as u64); vec.push(DataBlock(i as u64)); hashmap.insert(i as u64, DataBlock(i as u64)); } c.bench_function("level_block array", |b| b.iter(|| array_iter(black_box(&block_array)))); c.bench_function("small level_block array", |b| b.iter(|| small_array_iter(black_box(&small_block_array)))); //c.bench_function("cluster level_block array", |b| b.iter(|| cluster_array_iter(black_box(&cluster_block_array)))); c.bench_function("compact array", |b| b.iter(|| compact_array_iter(black_box(&compact_array)))); c.bench_function("vec", |b| b.iter(|| vec_iter(black_box(&vec)))); c.bench_function("hashmap", |b| b.iter(|| hashmap_iter(black_box(&hashmap)))); } criterion_group!(benches_iter, bench_iter); criterion_main!(benches_iter);