Created
August 7, 2023 04:01
-
-
Save ttsugriy/ac4ebba70949008f607dc0ae5cb7102f to your computer and use it in GitHub Desktop.
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::{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