Skip to content

Commit

Permalink
Use LazyLock instead of OnceLock for benchmark data
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Aug 21, 2024
1 parent ac6e046 commit d7b0a50
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,33 @@ macro_rules! benchmark {
($year:tt, $day:tt) => {
mod $day {
use aoc::$year::$day::*;
use std::fs;
use std::path::PathBuf;
use std::sync::OnceLock;
use std::fs::read_to_string;
use std::path::Path;
use std::sync::LazyLock;
use test::Bencher;

fn load_once() -> &'static str {
static DATA: OnceLock<String> = OnceLock::new();
DATA.get_or_init(|| {
let year = stringify!($year);
let day = &format!("{}.txt", stringify!($day));
let path: PathBuf = ["input", year, day].iter().collect();
fs::read_to_string(path).unwrap()
})
}
static DATA: LazyLock<String> = LazyLock::new(|| {
let year = stringify!($year);
let day = stringify!($day);
let path = Path::new("input").join(year).join(day).with_extension("txt");
read_to_string(path).unwrap()
});

#[bench]
fn parse_bench(b: &mut Bencher) {
let data = load_once();
let data = &DATA;
b.iter(|| parse(data));
}

#[bench]
fn part1_bench(b: &mut Bencher) {
let data = load_once();
let input = parse(data);
let input = parse(&DATA);
b.iter(|| part1(&input));
}

#[bench]
fn part2_bench(b: &mut Bencher) {
let data = load_once();
let input = parse(data);
let input = parse(&DATA);
b.iter(|| part2(&input));
}
}
Expand Down

0 comments on commit d7b0a50

Please sign in to comment.