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