Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created August 7, 2023 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttsugriy/ac4ebba70949008f607dc0ae5cb7102f to your computer and use it in GitHub Desktop.
Save ttsugriy/ac4ebba70949008f607dc0ae5cb7102f to your computer and use it in GitHub Desktop.
use criterion::{criterion_group, criterion_main, Criterion};
fn sort_by_words1(name: &str) -> String {
let mut split_words: Vec<&str> = name.split('_').collect();
// We are sorting primitive &strs and can use unstable sort here.
split_words.sort_unstable();
split_words.join("_")
}
fn sort_by_words2(name: &str) -> Vec<&str> {
let mut split_words: Vec<&str> = name.split('_').collect();
// We are sorting primitive &strs and can use unstable sort here.
split_words.sort_unstable();
split_words
}
fn bench_sorts(c: &mut Criterion) {
let mut group = c.benchmark_group("multiply add");
let name = "some_fancy_name";
group.bench_function("original", |b| b.iter(|| sort_by_words1(name)));
group.bench_function("proposed", |b| b.iter(|| sort_by_words2(name)));
group.finish();
}
criterion_group!(benches, bench_sorts);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment