benchmarks

This commit is contained in:
Niccolo Borgioli 2023-11-18 17:52:07 +01:00
parent eb723f5112
commit ad6ddf4f75
No known key found for this signature in database
GPG Key ID: D93C615F75EE4F0B
5 changed files with 109 additions and 9 deletions

View File

@ -1,5 +1,24 @@
fn main() { #![feature(test)]
// Part A
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a());
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b());
}
}
fn part_a() {
let result: u32 = include_str!("../input.txt") let result: u32 = include_str!("../input.txt")
.trim() .trim()
.split("\n\n") .split("\n\n")
@ -8,3 +27,19 @@ fn main() {
.unwrap(); .unwrap();
println!("A: {}", result); println!("A: {}", result);
} }
fn part_b() {
let mut result: Vec<u32> = include_str!("../input.txt")
.trim()
.split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum::<u32>())
.collect();
result.sort();
let total = &result.as_slice()[result.len() - 3..result.len()];
println!("B: {:?}", total.iter().sum::<u32>());
}
fn main() {
part_a();
part_b();
}

View File

@ -9,3 +9,5 @@ edition = "2021"
[[bin]] [[bin]]
name = "2022-01" name = "2022-01"
path = "./2022/01/rust/main.rs" path = "./2022/01/rust/main.rs"
# INJECT HERE

View File

@ -1,5 +1,37 @@
# Advent Of Code # Advent Of Code
> New days are generated with [`hygen`](https://github.com/jondot/hygen). ## Getting started
>
> `bun run gen --year 2022 --day 1` New "days" are generated with [`hygen`](https://github.com/jondot/hygen). This will bootstrap code for python and rust. See details about the specifica languages below
```bash
# First time setup
bun i
# Generate
bun run gen --year 2022 --day 1
```
## Python
## Rust
Run a single day:
```bash
cargo run --bin 2022-01
```
Bench a day:
```bash
cargo bench --bin 2022-01
```
Bench all days:
```bash
cargo bench
```
> The benchmark is the built in Rust one. This is the [reason](https://doc.rust-lang.org/cargo/commands/cargo-bench.html) for the _nightly_ version.

View File

@ -2,8 +2,37 @@
to: <%= dir %>/rust/main.rs to: <%= dir %>/rust/main.rs
unless_exists: true unless_exists: true
--- ---
fn main() { #![feature(test)]
// Part A
let contents = include_str!("../test.txt").trim(); extern crate test;
println!("A: {}", contents);
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a());
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b());
}
}
fn part_a() {
let result = include_str!("../input.txt");
println!("A: {}", result);
}
fn part_b() {
let result = include_str!("../input.txt");
println!("A: {}", result);
}
fn main() {
part_a();
part_b();
} }

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"