-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement: Faster benches across 2017
- Loading branch information
1 parent
9b4c470
commit 2c85383
Showing
23 changed files
with
380 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.