Try gaining performance.. fail

This commit is contained in:
Malte Tammena 2022-12-02 12:46:45 +01:00
parent 9e4c93628d
commit 43c1425903
6 changed files with 77 additions and 28 deletions

7
Cargo.lock generated
View file

@ -23,6 +23,13 @@ dependencies = [
name = "day02"
version = "0.1.0"
[[package]]
name = "day02-gen-problem"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "day03"
version = "0.1.0"

View file

@ -1,7 +1,8 @@
[workspace]
members = [ "day01", "day01-gen-problem", "day02", "day03", "day04", "day05", "day06", "day07", "day08", "day09", "day10", "day11", "day12", "day13", "day14", "day15", "day16", "day17", "day18", "day19", "day20", "day21", "day22", "day23", "day24", ]
members = [ "day01", "day01-gen-problem", "day02", "day02-gen-problem", "day03", "day04", "day05", "day06", "day07", "day08", "day09", "day10", "day11", "day12", "day13", "day14", "day15", "day16", "day17", "day18", "day19", "day20", "day21", "day22", "day23", "day24", ]
[profile.release]
#debug = true
panic = "abort"
strip = "symbols"
lto = true

View file

@ -0,0 +1,9 @@
[package]
name = "day02-gen-problem"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"

View file

@ -0,0 +1,29 @@
use std::{fs::File, io::BufWriter, io::Write};
use rand::Rng;
const LINE_COUNT: usize = 20_000_000;
fn main() {
let outfile = ::std::env::args()
.skip(1)
.next()
.expect("Output file as first parameter");
let mut outfile = BufWriter::new(File::create(outfile).expect("Can write outfile"));
let mut rng = rand::thread_rng();
(0..LINE_COUNT)
.map(|_| match rng.gen_range(1..=9) {
1 => "A X",
2 => "B X",
3 => "C X",
4 => "A Y",
5 => "B Y",
6 => "C Y",
7 => "A Z",
8 => "B Z",
9 => "C Z",
_ => unreachable!(),
})
.for_each(|line| writeln!(outfile, "{line}").expect("Writing works"));
outfile.flush().expect("Flushing works");
}

View file

@ -8,3 +8,6 @@ build = true
app = true
[dependencies]
[features]
first = []

View file

@ -3,37 +3,37 @@ use std::{
io::{BufRead, BufReader},
};
// fn calculate_game_score<S: AsRef<str>>(line: S) -> usize {
// let line = line.as_ref();
// match line {
// "A X" => 1 + 3, // Rock -- Rock
// "B X" => 1 + 0, // Paper -> Rock
// "C X" => 1 + 6, // Scissor <- Rock
// "A Y" => 2 + 6,
// "B Y" => 2 + 3,
// "C Y" => 2 + 0,
// "A Z" => 3 + 0,
// "B Z" => 3 + 6,
// "C Z" => 3 + 3,
// _ => unreachable!(),
// }
// }
fn calculate_game_score<S: AsRef<str>>(line: S) -> usize {
let line = line.as_ref();
fn calculate_game_score(line: String) -> usize {
let line = line.as_bytes();
let left = line[0] - b'A';
let right = line[2] - b'X';
#[cfg(feature = "first")]
match line {
(0, 0) => 1 + 3,
(1, 0) => 1 + 0,
(2, 0) => 1 + 6,
(0, 1) => 2 + 6,
(1, 1) => 2 + 3,
(2, 1) => 2 + 0,
(0, 2) => 3 + 0,
(1, 2) => 3 + 6,
(2, 2) => 3 + 3,
_ => unreachable!(),
}
#[cfg(not(feature = "first"))]
match (left, right) {
// Rock -> 1
// Paper -> 2
// Scissor -> 3
"A X" => 3 + 0,
"B X" => 1 + 0,
"C X" => 2 + 0,
"A Y" => 1 + 3,
"B Y" => 2 + 3,
"C Y" => 3 + 3,
"A Z" => 2 + 6,
"B Z" => 3 + 6,
"C Z" => 1 + 6,
(0, 0) => 3 + 0,
(1, 0) => 1 + 0,
(2, 0) => 2 + 0,
(0, 1) => 1 + 3,
(1, 1) => 2 + 3,
(2, 1) => 3 + 3,
(0, 2) => 2 + 6,
(1, 2) => 3 + 6,
(2, 2) => 1 + 6,
_ => unreachable!(),
}
}