Skip to content

Instantly share code, notes, and snippets.

@wpcarro
Last active January 1, 2025 04:18
Show Gist options
  • Save wpcarro/fe282ac51bf1725cac574cc1f773c0fd to your computer and use it in GitHub Desktop.
Save wpcarro/fe282ac51bf1725cac574cc1f773c0fd to your computer and use it in GitHub Desktop.
fn main() {
let mut rng = thread_rng();
println!("Vector");
for size in [1_000, 10_000, 50_000].into_iter() {
let mut xs = Vec::with_capacity(size);
for _ in 0..size {
xs.push(rng.gen_range(0..size));
}
let t0 = SystemTime::now();
let mut hits = 0;
let mut misses = 0;
for _ in 0..size {
let candidate = rng.gen_range(0..size);
if xs.contains(&candidate) {
hits += 1;
} else {
misses += 1;
}
}
let t1 = SystemTime::now();
println!("size: {size} - hits: {hits}; misses: {misses}; time: {:?}", t1.duration_since(t0).unwrap());
}
println!("HashSet");
for size in [1_000, 10_000, 50_000].into_iter() {
let mut xs = HashSet::with_capacity(size);
for _ in 0..size {
xs.insert(rng.gen_range(0..size));
}
let t0 = SystemTime::now();
let mut hits = 0;
let mut misses = 0;
for _ in 0..size {
let candidate = rng.gen_range(0..size);
if xs.contains(&candidate) {
hits += 1;
} else {
misses += 1;
}
}
let t1 = SystemTime::now();
println!("size: {size} - hits: {hits}; misses: {misses}; time: {:?}", t1.duration_since(t0).unwrap());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment