2014-07-15 18:39:32 -05:00
|
|
|
#![cfg(test)]
|
|
|
|
|
2019-11-13 17:32:36 -06:00
|
|
|
use std::collections::HashMap;
|
2019-11-27 12:29:00 -06:00
|
|
|
use test::Bencher;
|
2014-07-15 18:39:32 -05:00
|
|
|
|
|
|
|
#[bench]
|
2016-09-26 13:55:47 -05:00
|
|
|
fn new_drop(b: &mut Bencher) {
|
2014-07-15 18:39:32 -05:00
|
|
|
b.iter(|| {
|
2016-09-26 13:55:47 -05:00
|
|
|
let m: HashMap<i32, i32> = HashMap::new();
|
2014-07-15 18:39:32 -05:00
|
|
|
assert_eq!(m.len(), 0);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-09-26 13:55:47 -05:00
|
|
|
fn new_insert_drop(b: &mut Bencher) {
|
2014-07-15 18:39:32 -05:00
|
|
|
b.iter(|| {
|
|
|
|
let mut m = HashMap::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
m.insert(0, 0);
|
2014-07-15 18:39:32 -05:00
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-07-15 15:58:35 -05:00
|
|
|
fn grow_by_insertion(b: &mut Bencher) {
|
2014-07-15 18:39:32 -05:00
|
|
|
let mut m = HashMap::new();
|
|
|
|
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1..1001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut k = 1001;
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
m.insert(k, k);
|
|
|
|
k += 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn find_existing(b: &mut Bencher) {
|
|
|
|
let mut m = HashMap::new();
|
|
|
|
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1..1001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
b.iter(|| {
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1..1001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.contains_key(&i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn find_nonexisting(b: &mut Bencher) {
|
|
|
|
let mut m = HashMap::new();
|
|
|
|
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1..1001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
b.iter(|| {
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1001..2001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.contains_key(&i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn hashmap_as_queue(b: &mut Bencher) {
|
|
|
|
let mut m = HashMap::new();
|
|
|
|
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1..1001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(i, i);
|
|
|
|
}
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut k = 1;
|
2014-07-15 18:39:32 -05:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-11-06 11:24:47 -06:00
|
|
|
m.remove(&k);
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(k + 1000, k + 1000);
|
|
|
|
k += 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-11-06 11:24:47 -06:00
|
|
|
fn get_remove_insert(b: &mut Bencher) {
|
2014-07-15 18:39:32 -05:00
|
|
|
let mut m = HashMap::new();
|
|
|
|
|
2015-12-02 19:07:29 -06:00
|
|
|
for i in 1..1001 {
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(i, i);
|
|
|
|
}
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut k = 1;
|
2014-07-15 18:39:32 -05:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-11-06 11:24:47 -06:00
|
|
|
m.get(&(k + 400));
|
|
|
|
m.get(&(k + 2000));
|
|
|
|
m.remove(&k);
|
2014-07-15 18:39:32 -05:00
|
|
|
m.insert(k + 1000, k + 1000);
|
|
|
|
k += 1;
|
|
|
|
})
|
|
|
|
}
|