//! Most of the code here was taken directly from the `intmap` crate because I cannot be h\*cked to //! write benchmarks. //! //! Thus, here is the original license: //! //! Copyright (c) 2016 Jesper Axelsson //! Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: //! The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // ! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #![feature(test)] #[cfg(test)] mod tests { extern crate test; use indexmap::IndexMap; use intmap::IntMap; use inttable::{Entry, IntTable}; use std::collections::HashMap; use test::Bencher; const VEC_COUNT: usize = 10_000; // ********** Built in ********** #[bench] fn u64_insert_built_in(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = HashMap::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { test::black_box(map.insert(s, s)); } }); } #[bench] fn u64_insert_without_capacity_built_in(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); b.iter(|| { let mut map = HashMap::new(); for s in data.iter() { test::black_box(map.insert(s, s)); } test::black_box(&map); }); } #[bench] fn u64_get_built_in(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map: HashMap<&u64, &u64> = HashMap::with_capacity(data.len()); for s in data.iter() { test::black_box(map.insert(s, s)); } b.iter(|| { for s in data.iter() { #[allow(clippy::unit_arg)] // Thank you clippy, very cool test::black_box({ map.contains_key(s); }) } }); } // ********** IndexMap ********** #[bench] fn u64_insert_indexmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IndexMap::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { test::black_box(map.insert(s, s)); } }); } #[bench] fn u64_get_indexmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map: IndexMap<&u64, &u64> = IndexMap::with_capacity(data.len()); for s in data.iter() { test::black_box(map.insert(s, s)); } b.iter(|| { for s in data.iter() { #[allow(clippy::unit_arg)] // Thank you clippy, very cool test::black_box({ map.contains_key(s); }); } }); } // ********** Inttable ******** #[bench] fn u64_insert_inttable(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntTable::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { test::black_box(map.insert(*s, s)); } }); } #[bench] fn u64_insert_checked_inttable(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntTable::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { let _ = test::black_box(map.try_insert(*s, s)); } }); } #[bench] fn u64_insert_entry_inttable(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntTable::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { #[allow(clippy::unit_arg)] // Thank you clippy, very cool test::black_box(match map.entry(*s) { Entry::Occupied(_) => panic!("unexpected while insert, i = {}", s), Entry::Vacant(entry) => entry.insert(s), }); } }); } #[bench] fn u64_insert_without_capacity_inttable(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); b.iter(|| { let mut map = IntTable::new(); for s in data.iter() { test::black_box(map.insert(*s, s)); } test::black_box(&map); }); } #[bench] fn u64_resize_inttable(b: &mut Bencher) { b.iter(|| { let mut map: IntTable = IntTable::new(); map.reserve(VEC_COUNT); test::black_box(&map); }); } #[bench] fn u64_get_inttable(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntTable::with_capacity(data.len()); for s in data.iter() { map.insert(*s, s); } b.iter(|| { for s in data.iter() { test::black_box(map.contains_key(*s)); } }); } // ********** Intmap ********** #[bench] fn u64_insert_intmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntMap::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { test::black_box(map.insert(*s, s)); } }); } #[bench] fn u64_insert_checked_intmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntMap::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { test::black_box(map.insert_checked(*s, s)); } }); } #[bench] fn u64_insert_entry_intmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntMap::with_capacity(data.len()); b.iter(|| { map.clear(); for s in data.iter() { test::black_box(match map.entry(*s) { intmap::Entry::Occupied(_) => panic!("unexpected while insert, i = {}", s), intmap::Entry::Vacant(entry) => entry.insert(s), }); } }); } #[bench] fn u64_insert_without_capacity_intmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); b.iter(|| { let mut map = IntMap::new(); for s in data.iter() { test::black_box(map.insert(*s, s)); } test::black_box(&map); }); } #[bench] fn u64_resize_intmap(b: &mut Bencher) { b.iter(|| { let mut map: IntMap = IntMap::new(); map.reserve(VEC_COUNT); test::black_box(&map); }); } #[bench] fn u64_get_intmap(b: &mut Bencher) { let data = get_random_range(VEC_COUNT); let mut map = IntMap::with_capacity(data.len()); for s in data.iter() { map.insert(*s, s); } b.iter(|| { for s in data.iter() { test::black_box(map.contains_key(*s)); } }); } // ********** Misc ********** fn get_random_range(count: usize) -> Vec { use rand::prelude::StdRng; use rand::{Rng, SeedableRng}; let mut vec = Vec::new(); let mut rng = StdRng::seed_from_u64(4242); for _ in 0..count { vec.push(rng.gen::()); } vec.sort(); vec.dedup(); vec } }