advent-of-code/2021/day8/day8_rs/benches/unscramble.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

2021-12-09 21:27:35 +01:00
use std::{fs, path::PathBuf};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use day8_rs::{v1, v2, v3, v4};
pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("unscramble");
let mut data_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
data_file.push("inputs/input.txt");
let input = fs::read_to_string(data_file).unwrap();
let lines: Vec<&str> = input.lines().collect();
group.bench_function("v1", |b| {
b.iter(|| {
for line in &lines {
let _ = v1::unscramble(black_box(line));
}
})
});
group.bench_function("v2", |b| {
b.iter(|| {
for line in &lines {
let _ = v2::unscramble(black_box(line));
}
})
});
group.bench_function("v3", |b| {
b.iter(|| {
for line in &lines {
let _ = v3::unscramble(black_box(line));
}
})
});
group.bench_function("v4", |b| {
b.iter(|| {
for line in &lines {
let _ = unsafe { v4::unscramble(black_box(line)) };
2021-12-09 21:27:35 +01:00
}
})
});
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);