rust/library/std/benches/hash/map.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
1.6 KiB
Rust
Raw Normal View History

2014-07-15 18:39:32 -05:00
#![cfg(test)]
use std::collections::HashMap;
use test::Bencher;
2014-07-15 18:39:32 -05:00
#[bench]
fn new_drop(b: &mut Bencher) {
2014-07-15 18:39:32 -05:00
b.iter(|| {
let m: HashMap<i32, i32> = HashMap::new();
2014-07-15 18:39:32 -05:00
assert_eq!(m.len(), 0);
})
}
#[bench]
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]
fn grow_by_insertion(b: &mut Bencher) {
2014-07-15 18:39:32 -05:00
let mut m = HashMap::new();
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();
for i in 1..1001 {
2014-07-15 18:39:32 -05:00
m.insert(i, i);
}
b.iter(|| {
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();
for i in 1..1001 {
2014-07-15 18:39:32 -05:00
m.insert(i, i);
}
b.iter(|| {
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();
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(|| {
m.remove(&k);
2014-07-15 18:39:32 -05:00
m.insert(k + 1000, k + 1000);
k += 1;
});
}
#[bench]
fn get_remove_insert(b: &mut Bencher) {
2014-07-15 18:39:32 -05:00
let mut m = HashMap::new();
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(|| {
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;
})
}