Skip to content

Commit

Permalink
Improvement: Faster benches across 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 27, 2024
1 parent 9b4c470 commit 2c85383
Show file tree
Hide file tree
Showing 23 changed files with 380 additions and 166 deletions.
21 changes: 15 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ criterion = { version = "0.5.1", features = ["html_reports"] }
[[bench]]
name = "year_2015"
harness = false
[[bench]]
name = "year_2016"
harness = false
[[bench]]
name = "year_2017"
harness = false
# [[bench]]
# name = "year_2015_day_01"
# harness = false
Expand All @@ -57,9 +51,24 @@ harness = false
# [[bench]]
# name = "year_2015_day_05"
# harness = false
[[bench]]
name = "year_2016"
harness = false
# [[bench]]
# name = "year_2016_day_04"
# harness = false
# [[bench]]
# name = "year_2016_day_09"
# harness = false
[[bench]]
name = "year_2017"
harness = false
# [[bench]]
# name = "year_2017_day_01"
# harness = false
# [[bench]]
# name = "year_2017_day_02"
# harness = false
# [[bench]]
# name = "year_2017_day_04"
# harness = false
8 changes: 4 additions & 4 deletions benches/year_2015_day_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ fn move_character(mut pos: (i8, i8), direction: char) -> (i8, i8) {
pos
}

fn day_03_v1_bset(input: &str) -> usize {
fn day_03_v1_bset(input: &str) -> u16 {
let mut santa: (i8, i8) = (0, 0);
let mut houses = BTreeSet::from([santa]);

for (_idx, chr) in input.chars().enumerate() {
santa = move_character(santa, chr);
houses.insert(santa);
}
houses.len()
houses.len() as u16
}

fn day_03_v2_bset(input: &str) -> usize {
fn day_03_v2_bset(input: &str) -> u16 {
let mut santa: (i8, i8) = (0, 0);
let mut robot: (i8, i8) = (0, 0);
let mut houses = BTreeSet::from([santa]);
Expand All @@ -38,7 +38,7 @@ fn day_03_v2_bset(input: &str) -> usize {
houses.insert(santa);
houses.insert(robot);
}
houses.len()
houses.len() as u16
}

fn bench_year_2015_day_03_sets(c: &mut Criterion) {
Expand Down
89 changes: 89 additions & 0 deletions benches/year_2017_day_01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use advent_rs::year_2017::day_01;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::time::Duration;

fn day_01_v2_naive(input: impl Into<String>) -> u16 {
let characters = input
.into()
.trim_end()
.chars()
.map(|c| c as u8)
.collect::<Vec<_>>();
let length = characters.len();
let dem_length = length / 2;
characters
.iter()
.enumerate()
.filter_map(|(idx, chr)| {
if *chr == characters[(idx + dem_length) % length] {
Some((*chr - 48) as u16)
} else {
None
}
})
.sum()
}

fn day_01_v2_zip(input: impl Into<String>) -> u16 {
let characters = input
.into()
.trim_end()
.chars()
.map(|c| c as u8)
.collect::<Vec<_>>();
characters
.iter()
.zip(characters.iter().cycle().skip(characters.len() / 2))
.filter_map(|(lhs, rhs)| {
if lhs == rhs {
Some((lhs - 48) as u16)
} else {
None
}
})
.sum()
}

// fn bench_year_2017_day_01_v1(c: &mut Criterion) {
// let mut group = c.benchmark_group("year_2017::day_01_v1");
// group.warm_up_time(Duration::from_millis(100));
// let input = include_str!("../inputs/year_2017/day_01_input");
// group.bench_with_input(BenchmarkId::new("Naive", input.len()), input, |b, input| {
// b.iter(|| day_01_v1_naive(input))
// });
// assert_eq!(day_01::day_01_v1(input), 1_069);
// group.bench_with_input(
// BenchmarkId::new("Normal", input.len()),
// input,
// |b, input| b.iter(|| day_01::day_01_v1(input)),
// );
// group.bench_with_input(BenchmarkId::new("Fast", input.len()), input, |b, input| {
// b.iter(|| day_01_v1_fast(input))
// });
// group.finish();
// }

fn bench_year_2017_day_01_v2(c: &mut Criterion) {
let mut group = c.benchmark_group("year_2017::day_01_v2");
group.warm_up_time(Duration::from_millis(100));
let input = include_str!("../inputs/year_2017/day_01_input");
assert_eq!(day_01_v2_naive(input), 1_268);
assert_eq!(day_01_v2_zip(input), 1_268);
assert_eq!(day_01::day_01_v2(input), 1_268);
group.bench_with_input(BenchmarkId::new("Naive", input.len()), input, |b, input| {
b.iter(|| day_01_v2_naive(input))
});
group.bench_with_input(BenchmarkId::new("Zip", input.len()), input, |b, input| {
b.iter(|| day_01_v2_zip(input))
});
group.bench_with_input(
BenchmarkId::new("Normal", input.len()),
input,
|b, input| b.iter(|| day_01::day_01_v2(input)),
);

group.finish();
}

criterion_group!(bench_year_2017_day_01, bench_year_2017_day_01_v2);
criterion_main!(bench_year_2017_day_01);
40 changes: 40 additions & 0 deletions benches/year_2017_day_02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use advent_rs::year_2017::day_02;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use itertools::Itertools;
use std::time::Duration;

pub fn day_02_v1_naive(input: impl Into<String>) -> u16 {
input
.into()
.lines()
.map(|line| {
let numbers: Vec<_> = line
.split_whitespace()
.map(|number| number.parse::<u16>().unwrap())
.sorted()
.collect();
numbers.iter().max().unwrap() - numbers.iter().min().unwrap()
})
.sum()
}

fn bench_year_2017_day_02_v1(c: &mut Criterion) {
let mut group = c.benchmark_group("year_2017::day_02_v1");
group.warm_up_time(Duration::from_millis(100));
let input = include_str!("../inputs/year_2017/day_02_input");
assert_eq!(day_02::day_02_v1(input), 53_978);
assert_eq!(day_02_v1_naive(input), 53_978);
group.bench_with_input(BenchmarkId::new("Naive", input.len()), input, |b, input| {
b.iter(|| day_02_v1_naive(input))
});
group.bench_with_input(
BenchmarkId::new("Normal", input.len()),
input,
|b, input| b.iter(|| day_02::day_02_v1(input)),
);

group.finish();
}

criterion_group!(bench_year_2017_day_02, bench_year_2017_day_02_v1);
criterion_main!(bench_year_2017_day_02);
84 changes: 84 additions & 0 deletions benches/year_2017_day_04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use advent_rs::year_2017::day_04;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use itertools::Itertools;
use std::{collections::HashSet, time::Duration};

pub fn day_04_v1_naive(input: impl Into<String>) -> u16 {
input
.into()
.lines()
.filter(|line| {
line
.split_whitespace()
.sorted()
.group_by(|word| *word)
.into_iter()
.all(|(_word, group)| group.count() == 1)
})
.count() as u16
}

pub fn day_04_v1_sorted(input: impl Into<String>) -> u16 {
input
.into()
.lines()
.filter(|line| {
!line
.split_whitespace()
.sorted()
.collect::<Vec<_>>()
.windows(2)
.any(|pair| pair[0] == pair[1])
})
.count() as u16
}

pub fn day_04_v1_with_set(input: impl Into<String>) -> u16 {
input
.into()
.lines()
.filter(|line| {
let mut set = HashSet::new();
for word in line.split_whitespace() {
if set.contains(word) {
return false;
}
set.insert(word);
}
true
})
.count() as u16
}

fn bench_year_2017_day_04_v1(c: &mut Criterion) {
let mut group = c.benchmark_group("year_2017::day_04_v1");
group.warm_up_time(Duration::from_millis(100));
let input = include_str!("../inputs/year_2017/day_04_input");
assert_eq!(day_04::day_04_v1(input), 466);
assert_eq!(day_04_v1_naive(input), 466);
assert_eq!(day_04_v1_sorted(input), 466);
assert_eq!(day_04_v1_with_set(input), 466);
group.bench_with_input(BenchmarkId::new("Naive", input.len()), input, |b, input| {
b.iter(|| day_04_v1_naive(input))
});
group.bench_with_input(
BenchmarkId::new("Sorted", input.len()),
input,
|b, input| b.iter(|| day_04_v1_sorted(input)),
);
group.bench_with_input(
BenchmarkId::new("WithSet", input.len()),
input,
|b, input| b.iter(|| day_04_v1_with_set(input)),
);
group.bench_with_input(
BenchmarkId::new("Normal", input.len()),
input,
|b, input| b.iter(|| day_04::day_04_v1(input)),
);

group.finish();
}

criterion_group!(bench_year_2017_day_04, bench_year_2017_day_04_v1);
criterion_main!(bench_year_2017_day_04);
2 changes: 1 addition & 1 deletion src/year_2015.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub mod day_25;
/// assert_eq!(solution, Some("0".to_string()));
/// ```
pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part != 1 && part != 2 {
if part > 2 {
return None;
}
match day {
Expand Down
14 changes: 7 additions & 7 deletions src/year_2015/day_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn move_character(pos: &mut (i8, i8), direction: u8) {
// pos
}

pub fn day_03_v1(input: impl Into<String>) -> usize {
pub fn day_03_v1(input: impl Into<String>) -> u16 {
let mut santa: (i8, i8) = (0, 0);
let mut houses = HashSet::from([santa]);

Expand All @@ -56,10 +56,10 @@ pub fn day_03_v1(input: impl Into<String>) -> usize {
houses.insert(santa);
}

houses.len()
houses.len() as u16
}

pub fn day_03_v2(input: impl Into<String>) -> usize {
pub fn day_03_v2(input: impl Into<String>) -> u16 {
let mut santa: (i8, i8) = (0, 0);
let mut robot: (i8, i8) = (0, 0);
let mut houses = HashSet::from([santa]);
Expand All @@ -71,18 +71,18 @@ pub fn day_03_v2(input: impl Into<String>) -> usize {
houses.insert(robot);
}

houses.len()
houses.len() as u16
}

solvable!(day_03, day_03_v1, day_03_v2, usize);
solvable!(day_03, day_03_v1, day_03_v2, u16);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn works_with_samples_v1() {
let sample_one: [(&str, usize); 3] = [
let sample_one: [(&str, u16); 3] = [
(">", 2),
("^>v<", 4),
("^v^v^v^v^v", 2),
Expand All @@ -95,7 +95,7 @@ mod tests {

#[test]
fn works_with_samples_v2() {
let sample_two: [(&str, usize); 3] = [
let sample_two: [(&str, u16); 3] = [
("^v", 3),
("^>v<", 3),
("^v^v^v^v^v", 11),
Expand Down
10 changes: 5 additions & 5 deletions src/year_2015/day_05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ fn string_is_nice_v2(input: &str) -> bool {
twice_pair && repeated
}

pub fn day_05_v1(input: impl Into<String>) -> usize {
pub fn day_05_v1(input: impl Into<String>) -> u16 {
input
.into()
.lines()
.filter(|line| string_is_nice_v1(line))
.count()
.count() as u16
}

pub fn day_05_v2(input: impl Into<String>) -> usize {
pub fn day_05_v2(input: impl Into<String>) -> u16 {
input
.into()
.lines()
.filter(|line| string_is_nice_v2(line))
.count()
.count() as u16
}

solvable!(day_05, day_05_v1, day_05_v2, usize);
solvable!(day_05, day_05_v1, day_05_v2, u16);

#[cfg(test)]
mod tests {
Expand Down
6 changes: 3 additions & 3 deletions src/year_2015/day_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
true
}

pub fn day_16_v1(input: impl Into<String>) -> usize {
pub fn day_16_v1(input: impl Into<String>) -> u16 {
let ticker_tape = build_ticker_tape();
let mut index = 1;
for aunt in input.into().lines() {
Expand All @@ -89,7 +89,7 @@ pub fn day_16_v1(input: impl Into<String>) -> usize {
index
}

pub fn day_16_v2(input: impl Into<String>) -> usize {
pub fn day_16_v2(input: impl Into<String>) -> u16 {
let ticker_tape = build_ticker_tape();
let mut index = 1;
for aunt in input.into().lines() {
Expand All @@ -106,4 +106,4 @@ pub fn day_16_v2(input: impl Into<String>) -> usize {
index
}

solvable!(day_16, day_16_v1, day_16_v2, usize);
solvable!(day_16, day_16_v1, day_16_v2, u16);
Loading

0 comments on commit 2c85383

Please sign in to comment.