use rand::rngs::SmallRng; use rand::seq::SliceRandom; use rand::SeedableRng;
use std::hash::{Hash, Hasher};
use std::borrow::Borrow; use std::ops::Deref;
/// Use a consistently seeded Rng for benchmark stability fn small_rng() -> SmallRng { let seed = u64::from_le_bytes(*b"indexmap");
SmallRng::seed_from_u64(seed)
}
impl<T> Deref for OneShot<T> { type Target = T; fn deref(&self) -> &T {
&self.0
}
}
fn shuffled_keys<I>(iter: I) -> Vec<I::Item> where
I: IntoIterator,
{ letmut v = Vec::from_iter(iter); letmut rng = small_rng();
v.shuffle(&mut rng);
v
}
#[bench] fn insert_hashmap_string_10_000(b: &mut Bencher) { let c = 10_000;
b.iter(|| { letmut map = HashMap::with_capacity(c); for x in0..c {
map.insert(x.to_string(), ());
}
map
});
}
#[bench] fn insert_hashmap_string_oneshot_10_000(b: &mut Bencher) { let c = 10_000;
b.iter(|| { letmut map = HashMap::with_capacity(c); for x in0..c {
map.insert(OneShot(x.to_string()), ());
}
map
});
}
#[bench] fn insert_indexmap_string_10_000(b: &mut Bencher) { let c = 10_000;
b.iter(|| { letmut map = IndexMap::with_capacity(c); for x in0..c {
map.insert(x.to_string(), ());
}
map
});
}
#[bench] fn lookup_hashmap_10_000_exist_string(b: &mut Bencher) { let c = 10_000; letmut map = HashMap::with_capacity(c); let keys = shuffled_keys(0..c); for &key in &keys {
map.insert(key.to_string(), 1);
} let lookups = (5000..c).map(|x| x.to_string()).collect::<Vec<_>>();
b.iter(|| { letmut found = 0; for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
#[bench] fn lookup_hashmap_10_000_exist_string_oneshot(b: &mut Bencher) { let c = 10_000; letmut map = HashMap::with_capacity(c); let keys = shuffled_keys(0..c); for &key in &keys {
map.insert(OneShot(key.to_string()), 1);
} let lookups = (5000..c)
.map(|x| OneShot(x.to_string()))
.collect::<Vec<_>>();
b.iter(|| { letmut found = 0; for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
#[bench] fn lookup_indexmap_10_000_exist_string(b: &mut Bencher) { let c = 10_000; letmut map = IndexMap::with_capacity(c); let keys = shuffled_keys(0..c); for &key in &keys {
map.insert(key.to_string(), 1);
} let lookups = (5000..c).map(|x| x.to_string()).collect::<Vec<_>>();
b.iter(|| { letmut found = 0; for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
#[bench] fn lookup_indexmap_10_000_exist_string_oneshot(b: &mut Bencher) { let c = 10_000; letmut map = IndexMap::with_capacity(c); let keys = shuffled_keys(0..c); for &key in &keys {
map.insert(OneShot(key.to_string()), 1);
} let lookups = (5000..c)
.map(|x| OneShot(x.to_string()))
.collect::<Vec<_>>();
b.iter(|| { letmut found = 0; for key in &lookups {
found += map.get(key).is_some() as i32;
}
found
});
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-21)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.