Created
April 5, 2023 19:30
-
-
Save nyurik/4b71c7979cccc5a20d89ce27c0f1d433 to your computer and use it in GitHub Desktop.
Benchmark to evaluate linear vs dup-indexer performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | |
use dup_indexer::DupIndexer; | |
fn benchmark_strings(c: &mut Criterion) { | |
let mut group = c.benchmark_group("Strings"); | |
group.bench_function("String", |b| { | |
b.iter(|| { | |
let mut di = DupIndexer::new(); | |
for _ in 0..100 { | |
for val in 0..100 { | |
black_box(di.insert(val.to_string())); | |
} | |
} | |
black_box(di.into_vec()) | |
}) | |
}); | |
group.bench_function("String-linear", |b| { | |
b.iter(|| { | |
let mut di = Vec::new(); | |
for _ in 0..100 { | |
for val in 0..100 { | |
black_box(get_index_or_add(val.to_string(), &mut di)); | |
} | |
} | |
black_box(di) | |
}) | |
}); | |
fn get_index_or_add(s: String, v: &mut Vec<String>) -> usize { | |
match v.iter().position(|x| x == &s) { | |
Some(i) => i, | |
None => { | |
v.push(s); | |
v.len() - 1 | |
} | |
} | |
} | |
} | |
criterion_group!(benches, benchmark_strings); | |
criterion_main!(benches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment