#![feature(test)] extern crate test; #[cfg(test)] mod bench_tests { use rand::seq::SliceRandom; use rand::{self, Rng}; use sokoban::node_allocator::FromSlice; use sokoban::node_allocator::NodeAllocatorMap; use sokoban::*; use std::collections::BTreeMap; use std::collections::HashMap; use test::Bencher; const MAX_SIZE: usize = 20001; const NUM_BUCKETS: usize = MAX_SIZE >> 2; const NUM_NODES: usize = (MAX_SIZE << 1) + 1; type RBTree = RedBlackTree; type SHashMap = HashTable; type AVLTreeMap = AVLTree; type CritbitTree = Critbit; const NUM_BUCKETS_1K: usize = 1000; const NUM_NODES_1K: usize = (1001 << 1) + 1; type RBTree1K = RedBlackTree; type SHashMap1K = HashTable; type AVLTreeMap1K = AVLTree; type CritbitTree1K = Critbit; #[bench] fn bench_std_btree_map_insert_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = BTreeMap::new(); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_std_hash_map_insert_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = HashMap::new(); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_red_black_tree_insert_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = RBTree1K::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_hash_map_insert_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = SHashMap1K::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_critbit_insert_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = CritbitTree1K::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_avl_tree_insert_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = AVLTreeMap1K::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_red_black_tree_insert_1000_u128_stack(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = RBTree1K::new(); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_hash_map_insert_1000_u128_stack(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = SHashMap1K::new(); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_critbit_insert_1000_u128_stack(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = CritbitTree1K::new(); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_avl_tree_insert_1000_u128_stack(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = AVLTreeMap1K::new(); b.iter(|| { for v in 0..1000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_std_btree_map_insert_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = BTreeMap::new(); b.iter(|| { for v in 0..20000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_std_hash_map_insert_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = HashMap::new(); b.iter(|| { for v in 0..20000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_red_black_tree_insert_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = RBTree::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..20000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_hash_map_insert_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = SHashMap::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..20000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_critbit_insert_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = CritbitTree::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..20000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_sokoban_avl_tree_insert_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = AVLTreeMap::new_from_slice(buf.as_mut_slice()); b.iter(|| { for v in 0..20000 { m.insert(v as u128, rng.gen::()); } }) } #[bench] fn bench_std_btree_map_remove_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = BTreeMap::new(); let mut slice: Vec = (0..1000).collect(); slice.shuffle(&mut rng); for v in 0..1000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for k in slice.iter() { m.remove(k); } }) } #[bench] fn bench_std_hash_map_remove_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = HashMap::new(); let mut slice: Vec = (0..1000).collect(); slice.shuffle(&mut rng); for v in 0..1000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for k in slice.iter() { m.remove(k); } }) } #[bench] fn bench_sokoban_red_black_tree_remove_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = RBTree::new_from_slice(buf.as_mut_slice()); let mut slice: Vec = (0..1000).collect(); slice.shuffle(&mut rng); for v in 0..1000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for k in slice.iter() { m.remove(k); } }) } #[bench] fn bench_sokoban_hash_map_remove_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = SHashMap::new_from_slice(buf.as_mut_slice()); let mut slice: Vec = (0..1000).collect(); slice.shuffle(&mut rng); for v in 0..1000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for k in slice.iter() { m.remove(k); } }) } #[bench] fn bench_sokoban_critbit_remove_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = CritbitTree::new_from_slice(buf.as_mut_slice()); let mut slice: Vec = (0..1000).collect(); slice.shuffle(&mut rng); for v in 0..1000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for k in slice.iter() { m.remove(k); } }) } #[bench] fn bench_sokoban_avl_tree_remove_1000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = AVLTreeMap::new_from_slice(buf.as_mut_slice()); let mut slice: Vec = (0..1000).collect(); slice.shuffle(&mut rng); for v in 0..1000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for k in slice.iter() { m.remove(k); } }) } #[bench] fn bench_std_btree_map_lookup_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = BTreeMap::new(); for v in 0..20000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for v in 0..20000 { m.get(&v); } }) } #[bench] fn bench_std_hash_map_lookup_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut m = HashMap::new(); for v in 0..20000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for v in 0..20000 { m.get(&v); } }) } #[bench] fn bench_sokoban_red_black_tree_lookup_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = RBTree::new_from_slice(buf.as_mut_slice()); for v in 0..20000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for v in 0..20000 { m.get(&v); } }) } #[bench] fn bench_sokoban_hash_map_lookup_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = SHashMap::new_from_slice(buf.as_mut_slice()); for v in 0..20000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for v in 0..20000 { m.get(&v); } }) } #[bench] fn bench_sokoban_critbit_lookup_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = CritbitTree::new_from_slice(buf.as_mut_slice()); for v in 0..20000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for v in 0..20000 { m.get(&v); } }) } #[bench] fn bench_sokoban_avl_tree_lookup_20000_u128(b: &mut Bencher) { let mut rng = rand::thread_rng(); let mut buf = vec![0u8; std::mem::size_of::()]; let m = AVLTreeMap::new_from_slice(buf.as_mut_slice()); for v in 0..20000 { m.insert(v as u128, rng.gen::()); } b.iter(|| { for v in 0..20000 { m.get(&v); } }) } }