Skip to content

Commit

Permalink
Improve microbenchmarks.
Browse files Browse the repository at this point in the history
Add benchmark for FNV hashing.
  • Loading branch information
BurntSushi committed Nov 12, 2015
1 parent b3a2e97 commit 631e419
Show file tree
Hide file tree
Showing 4 changed files with 107,140 additions and 40 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ regex-syntax = "0.2"
utf8-ranges = "0.1"

[dev-dependencies]
fnv = "1.0"
lazy_static = "0.1"
quickcheck = "0.2"
rand = "0.3"

Expand Down
124 changes: 84 additions & 40 deletions benches/search.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,96 @@
#![feature(hashmap_hasher)]
#![feature(test)]

extern crate fnv;
extern crate fst;
#[macro_use] extern crate lazy_static;
extern crate test;

use std::collections::{BTreeSet, HashSet};
const STR_WORDS: &'static str = include_str!("./../data/words-100000");
const STR_WIKI_URLS: &'static str = include_str!("./../data/wiki-urls-100000");

use fst::raw::{Builder, Fst};
use test::Bencher;

const WORDS: &'static str = include_str!("./../data/words-10000");

fn get_words() -> Vec<String> {
WORDS.lines().map(|s| s.to_owned()).collect()
fn get_keys(s: &'static str) -> Vec<String> {
s.lines().map(str::to_owned).collect()
}

#[bench]
fn search_fst_one(b: &mut Bencher) {
let words = get_words();
let mut bfst = Builder::memory();
for word in &words {
bfst.add(word).unwrap();
}
let fst = Fst::from_bytes(bfst.into_inner().unwrap()).unwrap();
let mut i = 0;
b.iter(|| {
i = (i + 1) % words.len();
assert!(fst.contains(&words[i]));
})
lazy_static! {
static ref WORDS: Vec<String> = get_keys(STR_WORDS);
static ref WIKI_URLS: Vec<String> = get_keys(STR_WIKI_URLS);
}

#[bench]
fn search_hash_one(b: &mut Bencher) {
let words = get_words();
let set: HashSet<String> = words.clone().into_iter().collect();
let mut i = 0;
b.iter(|| {
i = (i + 1) % words.len();
assert!(set.contains(&words[i]));
})
}
macro_rules! search {
($name:ident, $keys:expr) => {
mod $name {
use std::collections::{BTreeSet, HashSet};
use std::collections::hash_state::DefaultState;

use fnv::FnvHasher;
use fst::raw::{Builder, Fst};
use test::Bencher;

#[bench]
fn search_btree_one(b: &mut Bencher) {
let words = get_words();
let set: BTreeSet<String> = words.clone().into_iter().collect();
let mut i = 0;
b.iter(|| {
i = (i + 1) % words.len();
assert!(set.contains(&words[i]));
})
#[bench]
fn fst_contains(b: &mut Bencher) {
lazy_static! {
static ref FST: Fst = {
let mut bfst = Builder::memory();
for word in $keys.iter() {
bfst.add(word).unwrap();
}
let bytes = bfst.into_inner().unwrap();
Fst::from_bytes(bytes).unwrap()
};
}
let mut i = 0;
b.iter(|| {
i = (i + 1) % $keys.len();
assert!(FST.contains_key(&$keys[i]));
})
}

#[bench]
fn hash_fnv_contains(b: &mut Bencher) {
lazy_static! {
static ref SET: HashSet<String, DefaultState<FnvHasher>> = {
$keys.clone().into_iter().collect()
};
}
let mut i = 0;
b.iter(|| {
i = (i + 1) % $keys.len();
assert!(SET.contains(&$keys[i]));
})
}

#[bench]
fn hash_sip_contains(b: &mut Bencher) {
lazy_static! {
static ref SET: HashSet<String> = {
$keys.clone().into_iter().collect()
};
}
let mut i = 0;
b.iter(|| {
i = (i + 1) % $keys.len();
assert!(SET.contains(&$keys[i]));
})
}

#[bench]
fn btree_contains(b: &mut Bencher) {
lazy_static! {
static ref SET: BTreeSet<String> = {
$keys.clone().into_iter().collect()
};
}
let mut i = 0;
b.iter(|| {
i = (i + 1) % $keys.len();
assert!(SET.contains(&$keys[i]));
})
}
}
}
}

search!(words, ::WORDS);
search!(wiki_urls, ::WIKI_URLS);
Loading

0 comments on commit 631e419

Please sign in to comment.