Created
September 15, 2018 13:52
-
-
Save loyd/c9bbd091524dea9c268e453c6ccc1573 to your computer and use it in GitHub Desktop.
Compare rust fast hashers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
count = 10000000 | |
len = 512 | |
name min max spent (s) | |
---- --- --- ---- | |
fnv 19525 19537 2.417305 | |
fx 19531 19532 0.48344 | |
*/ | |
extern crate fnv; | |
extern crate rustc_hash; | |
use fnv::FnvHasher; | |
use rustc_hash::FxHasher; | |
use std::hash::{Hash, Hasher}; | |
use std::time::Instant; | |
const N: usize = 512; | |
const K: usize = 10_000_000; | |
fn check<H: Hasher + Default>(name: &str) { | |
let mut counters = [0; N]; | |
let start = Instant::now(); | |
for i in 0..K { | |
let mut hasher = H::default(); | |
i.hash(&mut hasher); | |
counters[hasher.finish() as usize % N] += 1; | |
} | |
let dur = start.elapsed(); | |
let min = counters.iter().min().unwrap(); | |
let max = counters.iter().max().unwrap(); | |
let spent = dur.as_secs() as f64 + dur.subsec_micros() as f64 / 1_000_000.; | |
println!("{}\t{}\t{}\t{}", name, min, max, spent); | |
} | |
fn main() { | |
println!("count = {}\nlen = {}\n\n", K, N); | |
println!("name\tmin\tmax\tspent (s)\n----\t---\t---\t----"); | |
check::<FnvHasher>("fnv"); | |
check::<FxHasher>("fx"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment