Created
June 2, 2017 23:10
-
-
Save anonymous/d0dd3b9a1c2e8403e441820c62e6e3d7 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
#[cfg(test)] | |
mod benchmarks { | |
extern crate test; | |
extern crate uuid; | |
use std::collections::HashMap; | |
use self::uuid::Uuid; | |
use self::test::Bencher; | |
const HASH_SIZE: usize = 1 << 16; | |
const HASH_CAPACITY: usize = 1 << 8; | |
fn get_random_hash<T>(new: &Fn(usize) -> T, insert: &Fn(&mut T, String, usize) -> ()) -> (T, Vec<String>) { | |
let mut keys = Vec::with_capacity(HASH_SIZE); | |
let mut hash = new(HASH_CAPACITY); | |
for i in 0..HASH_SIZE { | |
let k: String = format!("{}", Uuid::new_v4()); | |
keys.push(k.clone()); | |
insert(&mut hash, k, i); | |
} | |
return (hash, keys); | |
} | |
fn rust_insert_fn(map: &mut HashMap<String, usize>, key: String, value: usize) { | |
map.insert(key, value); | |
} | |
#[bench] | |
fn rust_insert(b: &mut Bencher) { | |
b.iter(|| { | |
let mut hash = HashMap::with_capacity(HASH_CAPACITY); | |
for i in 0..HASH_SIZE { | |
let k: String = format!("{}", Uuid::new_v4()); | |
hash.insert(k, i); | |
} | |
}); | |
} | |
#[bench] | |
fn rust_get(b: &mut Bencher) { | |
let (hash, keys) = get_random_hash::<HashMap<String, usize>>(&HashMap::with_capacity, &rust_insert_fn); | |
let mut keys = test::black_box(keys); | |
b.iter(|| { | |
let mut n = 0; | |
for k in keys.drain(..) { | |
hash.get(&k); | |
n += 1; | |
}; | |
return n; | |
}); | |
} | |
#[bench] | |
fn rust_get_random(b: &mut Bencher) { | |
let (hash, _) = get_random_hash::<HashMap<String, usize>>(&HashMap::with_capacity, &rust_insert_fn); | |
b.iter(|| { | |
for _ in 0..HASH_SIZE { | |
hash.get(&format!("{}", Uuid::new_v4())); | |
} | |
}); | |
} | |
#[bench] | |
fn rust_del(b: &mut Bencher) { | |
let (mut hash, keys) = get_random_hash::<HashMap<String, usize>>(&HashMap::with_capacity, &rust_insert_fn); | |
let mut keys = test::black_box(keys); | |
b.iter(|| { | |
for k in keys.drain(..) { | |
hash.remove(&k); | |
}; | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment