-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
107,140 additions
and
40 deletions.
There are no files selected for viewing
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
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
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); |
Oops, something went wrong.