Created
October 29, 2023 21:24
-
-
Save llogiq/72f3bb129cb27d6d30384f4424eb5085 to your computer and use it in GitHub Desktop.
Small starts_with benchmark
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
/* Put this into Cargo.toml and the below code under benches/bench.rs: | |
[package] | |
name = "startbench" | |
version = "0.1.0" | |
edition = "2021" | |
[[bench]] | |
name = "bench" | |
harness = false | |
[dependencies] | |
criterion = "0.5.1" | |
*/ | |
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | |
pub fn benchmark(c: &mut Criterion) { | |
let chars = ['a', 'ö', '\u{1F609}']; | |
let mut char_group = c.benchmark_group("char"); | |
for (i, t) in chars.iter().enumerate() { | |
char_group.bench_with_input(BenchmarkId::from_parameter(i), t, |b, &t| { | |
b.iter(|| black_box("whoa!".starts_with(black_box(t)))) | |
}); | |
} | |
char_group.finish(); | |
let mut str_group = c.benchmark_group("&str"); | |
for (i, t) in chars.into_iter().enumerate() { | |
let s = t.to_string(); | |
str_group.bench_with_input(BenchmarkId::from_parameter(i), &s[..], |b, s| { | |
b.iter(|| black_box("whoa!".starts_with(black_box(s)))) | |
}); | |
} | |
str_group.finish(); | |
} | |
criterion_group!(benches, benchmark); | |
criterion_main!(benches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment