Skip to content

Instantly share code, notes, and snippets.

@bluss bluss/u32hash.rs Secret
Created Mar 14, 2017

Embed
What would you like to do?
//! ```cargo
//! [dependencies]
//! seahash = "3.0"
//! ordermap = "0.2.8"
//! fnv = "1.0"
//! rand = "0.3"
//! bencher = "0.1"
//! ```
#[macro_use]
extern crate bencher;
extern crate rand;
extern crate fnv;
extern crate seahash;
use fnv::FnvHasher;
use std::hash::BuildHasherDefault;
type FnvBuilder = BuildHasherDefault<FnvHasher>;
type ShBuilder = BuildHasherDefault<seahash::SeaHasher>;
use bencher::Bencher;
extern crate ordermap;
//use ordermap::OrderMap;
use std::collections::HashMap;
use std::iter::FromIterator;
use rand::{weak_rng, Rng};
fn shuffled_keys<I>(iter: I) -> Vec<I::Item>
where I: IntoIterator
{
let mut v = Vec::from_iter(iter);
let mut rng = weak_rng();
rng.shuffle(&mut v);
v
}
type KeyType = u32;
fn lookup_siphash(b: &mut Bencher) {
let c = 10_000 as KeyType;
let mut map = HashMap::with_capacity(c as usize);
let keys = shuffled_keys(0..c);
for &key in &keys {
map.insert(key, 1);
}
let lookups = (5000..c).collect::<Vec<_>>();
b.iter(|| {
let mut found = 0;
for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
fn lookup_fnv(b: &mut Bencher) {
let c = 10_000 as KeyType;
let mut map = HashMap::with_capacity_and_hasher(c as usize, FnvBuilder::default());
let keys = shuffled_keys(0..c);
for &key in &keys {
map.insert(key, 1);
}
let lookups = (5000..c).collect::<Vec<_>>();
b.iter(|| {
let mut found = 0;
for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
fn lookup_seahash(b: &mut Bencher) {
let c = 10_000 as KeyType;
let mut map = HashMap::with_capacity_and_hasher(c as usize, ShBuilder::default());
let keys = shuffled_keys(0..c);
for &key in &keys {
map.insert(key, 1);
}
let lookups = (5000..c).collect::<Vec<_>>();
b.iter(|| {
let mut found = 0;
for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
benchmark_group!(benches,
lookup_siphash,
lookup_fnv,
lookup_seahash);
benchmark_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.