Advertisement
Guest User

Hashing Benchmarks for u64 keys

a guest
Oct 16th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.78 KB | None | 0 0
  1. ============================================== PROJECT_DIR/Cargo.toml ========================================
  2.  
  3. [package]
  4. name = "tests"
  5. version = "0.1.0"
  6. edition = "2018"
  7.  
  8. [dev-dependencies]
  9. criterion = { version = "0.3", features = ["real_blackbox"] } # you can remove this feature if you're not on nightly
  10.  
  11. [dependencies]
  12. ahash = "0.7"
  13. rustc-hash = "1"
  14. rand = "0.8"
  15.  
  16. [[bench]]
  17. name = "benchmark"
  18. harness = false
  19.  
  20.  
  21. ============================================== PROJECT_DIR/benches/benchmark.rs ===============================
  22.  
  23. use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
  24.  
  25. use ahash::AHasher;
  26. use rand::distributions::Standard;
  27. use rand::prelude::Distribution;
  28. use rand::prelude::SliceRandom;
  29. use rand::Rng;
  30. use rustc_hash::FxHasher;
  31. use std::collections::HashSet;
  32. use std::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher};
  33. use std::iter::FromIterator;
  34.  
  35. pub trait Set {
  36.    type Item;
  37.  
  38.    fn from_iterator<I: IntoIterator<Item = Self::Item>>(values: I) -> Self;
  39.  
  40.    fn get(&self, item: &Self::Item) -> bool;
  41. }
  42.  
  43. impl<T, S> Set for HashSet<T, S>
  44. where
  45.    T: Eq + Hash,
  46.    S: Default + BuildHasher,
  47. {
  48.    type Item = T;
  49.  
  50.    #[inline]
  51.    fn from_iterator<I: IntoIterator<Item = Self::Item>>(values: I) -> Self {
  52.        FromIterator::from_iter(values)
  53.    }
  54.  
  55.    #[inline]
  56.    fn get(&self, item: &Self::Item) -> bool {
  57.        self.contains(item)
  58.    }
  59. }
  60.  
  61. pub struct LinearSearch<T>(pub Vec<T>);
  62.  
  63. impl<T> Set for LinearSearch<T>
  64. where
  65.    T: PartialEq,
  66. {
  67.    type Item = T;
  68.  
  69.    #[inline]
  70.    fn from_iterator<I: IntoIterator<Item = Self::Item>>(values: I) -> Self {
  71.        Self(FromIterator::from_iter(values))
  72.    }
  73.  
  74.    #[inline]
  75.    fn get(&self, item: &Self::Item) -> bool {
  76.        self.0.contains(item)
  77.    }
  78. }
  79.  
  80. pub struct BinarySearch<T>(pub Vec<T>);
  81.  
  82. impl<T> Set for BinarySearch<T>
  83. where
  84.    T: Ord,
  85. {
  86.    type Item = T;
  87.  
  88.    #[inline]
  89.    fn from_iterator<I: IntoIterator<Item = Self::Item>>(values: I) -> Self {
  90.        let mut vec = Vec::from_iter(values);
  91.        vec.sort_unstable();
  92.        Self(vec)
  93.    }
  94.  
  95.    #[inline]
  96.    fn get(&self, item: &Self::Item) -> bool {
  97.        self.0.binary_search(item).is_ok()
  98.    }
  99. }
  100.  
  101. #[derive(Default)]
  102. pub struct NoHasher(pub u64);
  103.  
  104. impl Hasher for NoHasher {
  105.    #[inline]
  106.    fn finish(&self) -> u64 {
  107.        self.0
  108.    }
  109.  
  110.    #[inline]
  111.    fn write_u64(&mut self, i: u64) {
  112.        self.0 = i;
  113.    }
  114.  
  115.    fn write(&mut self, _bytes: &[u8]) {
  116.        unreachable!();
  117.    }
  118.  
  119.    fn write_i128(&mut self, _i: i128) {
  120.        unreachable!();
  121.    }
  122.  
  123.    fn write_i16(&mut self, _i: i16) {
  124.        unreachable!();
  125.    }
  126.  
  127.    fn write_i32(&mut self, _i: i32) {
  128.        unreachable!();
  129.    }
  130.  
  131.    fn write_i64(&mut self, _i: i64) {
  132.        unreachable!();
  133.    }
  134.  
  135.    fn write_i8(&mut self, _i: i8) {
  136.        unreachable!();
  137.    }
  138.  
  139.    fn write_isize(&mut self, _i: isize) {
  140.        unreachable!();
  141.    }
  142.    fn write_u128(&mut self, _i: u128) {
  143.        unreachable!();
  144.    }
  145.    fn write_u16(&mut self, _i: u16) {
  146.        unreachable!();
  147.    }
  148.  
  149.    fn write_u32(&mut self, _i: u32) {
  150.        unreachable!();
  151.    }
  152.    fn write_u8(&mut self, _i: u8) {
  153.        unreachable!();
  154.    }
  155.  
  156.    fn write_usize(&mut self, _i: usize) {
  157.        unreachable!();
  158.    }
  159. }
  160.  
  161. fn bench_set<S>(items: &[S::Item], b: &mut Bencher)
  162. where
  163.    S: Set,
  164.    S::Item: Clone,
  165.    Standard: Distribution<S::Item>,
  166. {
  167.    let set = S::from_iterator(items.iter().cloned());
  168.    let against = items.choose(&mut rand::thread_rng()).unwrap();
  169.    b.iter(|| black_box(set.get(black_box(&against))))
  170. }
  171.  
  172. pub fn bench(c: &mut Criterion) {
  173.    let mut group = c.benchmark_group("Hit");
  174.  
  175.    let items: Vec<u64> = rand::thread_rng().sample_iter(Standard).take(32).collect();
  176.  
  177.    let nums = [1usize, 2, 4, 8, 16, 32];
  178.    for i in nums {
  179.        group.bench_with_input(BenchmarkId::new("ahash", i), &i, |b, i| {
  180.            bench_set::<HashSet<u64, BuildHasherDefault<AHasher>>>(&items[..*i], b)
  181.        });
  182.  
  183.        group.bench_with_input(BenchmarkId::new("fxhash", i), &i, |b, i| {
  184.            bench_set::<HashSet<u64, BuildHasherDefault<FxHasher>>>(&items[..*i], b)
  185.        });
  186.  
  187.        group.bench_with_input(BenchmarkId::new("nohash", i), &i, |b, i| {
  188.            bench_set::<HashSet<u64, BuildHasherDefault<NoHasher>>>(&items[..*i], b)
  189.        });
  190.  
  191.        group.bench_with_input(BenchmarkId::new("linear", i), &i, |b, i| {
  192.            bench_set::<LinearSearch<u64>>(&items[..*i], b)
  193.        });
  194.  
  195.        group.bench_with_input(BenchmarkId::new("binary", i), &i, |b, i| {
  196.            bench_set::<BinarySearch<u64>>(&items[..*i], b)
  197.        });
  198.    }
  199. }
  200.  
  201. criterion_group!(benches, bench);
  202. criterion_main!(benches);
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement