SHARE
TWEET

Untitled

a guest Mar 20th, 2019 76 in 242 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[macro_use]
  2. extern crate criterion;
  3.  
  4. use criterion::Criterion;
  5. use hashbrown::HashMap;
  6. use parking_lot::RwLock;
  7. use rayon::prelude::*;
  8. // use std::sync::Arc;
  9. use chashmap::CHashMap;
  10.  
  11. const ITER_NUM: usize = 10000;
  12.  
  13. fn std_hashmap_multi() {
  14.     let v: RwLock<std::collections::HashMap<u64, u128>> = RwLock::new(std::collections::HashMap::new());
  15.  
  16.     (0..ITER_NUM).collect::<Vec<usize>>().par_iter().for_each(|i| {
  17.         let mut lc = v.write();
  18.         lc.insert(*i as u64, *i as u128 + 8);
  19.     });
  20.  
  21.     (0..ITER_NUM).collect::<Vec<usize>>().par_iter().for_each(|i| {
  22.         let mut lc = v.write();
  23.         let r = lc.get_mut(&(*i as u64)).unwrap();
  24.         *r += 83;
  25.     });
  26. }
  27.  
  28. fn chashmap_hashmap() {
  29.     let mut v: CHashMap<u64, u128> = CHashMap::new();
  30.  
  31.     (0..ITER_NUM).for_each(|i| {
  32.         v.insert(i as u64, i as u128 + 8);
  33.     });
  34.  
  35.     (0..ITER_NUM).for_each(|i| {
  36.         let mut r = v.get_mut(&(i as u64)).unwrap();
  37.         *r += 83;
  38.     });
  39. }
  40.  
  41. fn chashmap_hashmap_multi() {
  42.     let v: CHashMap<u64, u128> = CHashMap::new();
  43.  
  44.     (0..ITER_NUM).collect::<Vec<usize>>().par_iter().for_each(|i| {
  45.         v.insert(*i as u64, *i as u128 + 8);
  46.     });
  47.  
  48.     (0..ITER_NUM).collect::<Vec<usize>>().par_iter().for_each(|i| {
  49.         let index = (*i as u64);
  50.         let mut r = v.get_mut(&index).unwrap();
  51.         *r += 83;
  52.     });
  53. }
  54.  
  55. fn hashbrown_hashmap() {
  56.     let mut v: HashMap<u64, u128> = HashMap::new();
  57.  
  58.     (0..ITER_NUM).for_each(|i| {
  59.         v.insert(i as u64, i as u128 + 8);
  60.     });
  61.  
  62.     (0..ITER_NUM).for_each(|i| {
  63.         let r = v.get_mut(&(i as u64)).unwrap();
  64.         *r += 83;
  65.     });
  66. }
  67.  
  68. fn hashbrown_hashmap_multi() {
  69.     let v: RwLock<HashMap<u64, u128>> = RwLock::new(HashMap::new());
  70.  
  71.     (0..ITER_NUM).collect::<Vec<usize>>().par_iter().for_each(|i| {
  72.         let mut lc = v.write();
  73.         lc.insert(*i as u64, *i as u128 + 8);
  74.     });
  75.  
  76.     (0..ITER_NUM).collect::<Vec<usize>>().par_iter().for_each(|i| {
  77.         let mut lc = v.write();
  78.         let r = lc.get_mut(&(*i as u64)).unwrap();
  79.         *r += 83;
  80.     });
  81. }
  82.  
  83. fn criterion_benchmark(c: &mut Criterion) {
  84.     c.bench_function("hashbrown_hashmap", |b| b.iter(|| hashbrown_hashmap()));
  85.     c.bench_function("chashmap_hashmap", |b| b.iter(|| chashmap_hashmap()));
  86.     c.bench_function("hashbrown_hashmap_multi", |b| b.iter(|| hashbrown_hashmap_multi()));
  87.     c.bench_function("chashmap_hashmap_multi", |b| b.iter(|| chashmap_hashmap_multi()));
  88.     c.bench_function("std_hashmap_multi", |b| b.iter(|| std_hashmap_multi()));
  89. }
  90.  
  91. criterion_group!(benches, criterion_benchmark);
  92. criterion_main!(benches);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top