From ee741775fec92f06567621e451859632db2818c6 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 26 Dec 2020 00:33:18 +0100 Subject: [PATCH] rust: Improve benchmarks and optimize day 6 a little --- README.md | 32 ++++++++++++++++---------------- rust/src/day06.rs | 6 ++++-- rust/src/main.rs | 5 +++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 1bbd720..ff4ec35 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Days 6 and 12 are missing because I did them in IPython and never bothered putti | 22 | | ✔️ | | | 23 | ✔️ | ✔️ | | | 24 | | ✔️ | | -| 25 | | | | +| 25 | ✔️ | | | ## Benchmarks (Rust) @@ -39,29 +39,29 @@ I'm somewhat trying to do the <1 second challenge, but I've been a bit short on | Day | Time | | ---------: | -----------: | -| 1 | 111.14µs | -| 2 | 110.91µs | -| 3 | 58.66µs | -| 4 | 280.38µs | -| 5 | 65.60µs | -| 6 | - | +| 1 | 112.46µs | +| 2 | 113.41µs | +| 3 | 60.96µs | +| 4 | 286.32µs | +| 5 | 73.25µs | +| 6 | 78.59µs | | 7 | - | -| 8 | 168.35µs | -| 9 | 45.37µs | +| 8 | 170.56µs | +| 9 | 46.69µs | | 10 | - | | 11 | - | | 12 | - | | 13 | - | | 14 | - | -| 15 | 475441.39µs | -| 16 | 338.97µs | -| 17 | 69548.73µs | +| 15 | 459975.24µs | +| 16 | 344.11µs | +| 17 | 70782.00µs | | 18 | - | -| 19 | 398.30µs | +| 19 | 403.49µs | | 20 | - | | 21 | - | | 22 | - | -| 23 | 196018.58µs | +| 23 | 195819.54µs | | 24 | - | -| 25 | 26280.61µs | -| **Total:** | 768.867ms | +| 25 | 26266.60µs | +| **Total:** | 754.533ms | diff --git a/rust/src/day06.rs b/rust/src/day06.rs index 45dd10b..acd916a 100644 --- a/rust/src/day06.rs +++ b/rust/src/day06.rs @@ -13,7 +13,7 @@ pub fn run(print: bool) -> Duration { let mut part1 = 0; let mut part2 = 0; - for group in file_string.trim().split("\n\n") { + for group in file_string.split("\n\n") { let mut set = 0u32; for c in group.chars().filter(|c| c.is_alphabetic()) { set |= 1 << (c as usize - 'a' as usize); @@ -29,7 +29,9 @@ pub fn run(print: bool) -> Duration { line_set |= 1 << (c as usize - 'a' as usize); } } - set &= line_set; + if line_set != 0 { + set &= line_set; + } part2 += count_bits(set); } diff --git a/rust/src/main.rs b/rust/src/main.rs index 896ce08..2eec5fc 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -73,12 +73,13 @@ fn main() { } let repetitions = - 5000.min((Duration::new(2, 0).as_nanos() / first_run.as_nanos()) as u32); + 100000.min((Duration::new(5, 0).as_nanos() / first_run.as_nanos()) as u32); let mut elapsed = Duration::new(0, 0); for _ in 0..repetitions { - elapsed += days[day](false) / repetitions; + elapsed += days[day](false); } + elapsed /= repetitions; println!("| {:>2} | {:>10.2}µs |", day + 1, elapsed.as_secs_f64() * 1_000_000.0); total += elapsed; }