Put benchmarks in own module and added 6MB version of Day 5 problem to see how it performs

This commit is contained in:
Dreaded_X 2022-12-06 03:47:31 +01:00
parent 3047033aa3
commit f03ddf12cf
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
7 changed files with 200061 additions and 15 deletions

200001
2022/input/5/large Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@ fn main() -> Result<()> {
Day::solve()
}
// -- Tests --
#[cfg(test)]
mod tests {
use super::*;
@ -27,17 +28,23 @@ mod tests {
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 206582)
}
}
// -- Benchmarks --
#[cfg(test)]
mod bench {
use super::*;
// Benchmarks
extern crate test;
#[bench]
#[ignore]
fn part1_bench(b: &mut test::Bencher) {
fn part1_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::ONE, b)
}
#[bench]
#[ignore]
fn part2_bench(b: &mut test::Bencher) {
fn part2_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::TWO, b)
}
}

View File

@ -27,17 +27,23 @@ mod tests {
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 12382)
}
}
// -- Benchmarks --
#[cfg(test)]
mod bench {
use super::*;
// Benchmarks
extern crate test;
#[bench]
#[ignore]
fn part1_bench(b: &mut test::Bencher) {
fn part1_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::ONE, b)
}
#[bench]
#[ignore]
fn part2_bench(b: &mut test::Bencher) {
fn part2_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::TWO, b)
}
}

View File

@ -27,17 +27,23 @@ mod tests {
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 2708)
}
}
// -- Benchmarks --
#[cfg(test)]
mod bench {
use super::*;
// Benchmarks
extern crate test;
#[bench]
#[ignore]
fn part1_bench(b: &mut test::Bencher) {
fn part1_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::ONE, b)
}
#[bench]
#[ignore]
fn part2_bench(b: &mut test::Bencher) {
fn part2_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::TWO, b)
}
}

View File

@ -28,17 +28,23 @@ mod tests {
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 907)
}
}
// -- Benchmarks --
#[cfg(test)]
mod bench {
use super::*;
// Benchmarks
extern crate test;
#[bench]
#[ignore]
fn part1_bench(b: &mut test::Bencher) {
fn part1_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::ONE, b)
}
#[bench]
#[ignore]
fn part2_bench(b: &mut test::Bencher) {
fn part2_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::TWO, b)
}
}

View File

@ -12,6 +12,7 @@ fn main() -> Result<()> {
Day::solve()
}
// -- Tests --
#[cfg(test)]
mod tests {
use super::*;
@ -33,16 +34,35 @@ mod tests {
Day::test(aoc::Part::TWO, "input", "CNSFCGJSM".to_string())
}
// Run it on a 6MB file to see how it performs
// https://www.reddit.com/r/adventofcode/comments/zd1hqy/2022_day_5_i_know_i_am_overthinking_it/iyzvsnp/?context=3
#[test]
#[ignore]
fn part1_large() -> Result<()> {
Day::test(aoc::Part::ONE, "large", "GATHERING".to_string())
}
#[test]
#[ignore]
fn part2_large() -> Result<()> {
Day::test(aoc::Part::TWO, "large", "DEVSCHUUR".to_string())
}
}
// -- Benchmarks --
#[cfg(test)]
mod bench {
use super::*;
// Benchmarks
extern crate test;
#[bench]
#[ignore]
fn part1_bench(b: &mut test::Bencher) {
fn part1_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::ONE, b)
}
#[bench]
#[ignore]
fn part2_bench(b: &mut test::Bencher) {
fn part2_solution(b: &mut test::Bencher) {
Day::benchmark(aoc::Part::TWO, b)
}
}
@ -137,7 +157,7 @@ fn parse_instruction(s: &str) -> (usize, usize, usize) {
(amount, from, to)
}
fn solution(input: &str, reverse: bool) -> String {
fn solution(input: &str, part1: bool) -> String {
// The current layout description ends with an empty line
let mut boat = Boat::new(&input
.lines()
@ -151,9 +171,9 @@ fn solution(input: &str, reverse: bool) -> String {
.map(parse_instruction)
.for_each(|(amount, from, to)| {
let mut taken = boat.take(from, amount);
// The order needs to be reversed in part due to the crates being moves one at the time
// instead of the whole stack at once as happens in part 2
if reverse {
if part1 {
// In part one we move the crates on by one, so the vector needs to be reverse to
// get the correct order
taken.reverse();
}
boat.put(to, taken);

View File

@ -47,9 +47,9 @@ pub trait Solver {
fn benchmark(part: Part, b: &mut test::Bencher) {
let f = Self::select(part);
let input = fs::read_to_string(format!("input/{}/input", Self::day())).with_context(|| format!("Failed to read 'input' for day {}", Self::day())).unwrap();
b.iter(|| {
let input = fs::read_to_string(format!("input/{}/input", Self::day())).unwrap();
f(&input)
});
}