Skip to content

Commit 335f263

Browse files
authored
refactor: remove --time flags in favor of cargo time command (#58)
1 parent 234ac70 commit 335f263

File tree

6 files changed

+48
-36
lines changed

6 files changed

+48
-36
lines changed

README.md

+26-9
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ cargo solve <day>
8989

9090
The `solve` command runs your solution against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program.
9191

92-
By default, `solve` executes your code once and shows the execution time. If you append the `--time` flag to the command, the runner will run your code between `10` and `10.000` times (depending on execution time of first execution) and print the average execution time.
93-
94-
For example, running a benchmarked, optimized execution of day 1 would look like `cargo solve 1 --release --time`. Displayed _timings_ show the raw execution time of your solution without overhead like file reads.
95-
9692
#### Submitting solutions
9793

9894
> [!IMPORTANT]
@@ -116,15 +112,36 @@ cargo all
116112
# Total: 0.20ms
117113
```
118114

119-
This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build and the `--time` flag outputs benchmarks.
115+
This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build.
116+
117+
### ➡️ Benchmark your solutions
118+
119+
```sh
120+
# example: `cargo time 8 --store`
121+
cargo time <day> [--all] [--store]
122+
123+
# output:
124+
# Day 08
125+
# ------
126+
# Part 1: 1 (39.0ns @ 10000 samples)
127+
# Part 2: 2 (39.0ns @ 10000 samples)
128+
#
129+
# Total (Run): 0.00ms
130+
#
131+
# Stored updated benchmarks.
132+
```
133+
134+
The `cargo time` command allows you to benchmark your code and store timings in the readme. When benching, the runner will run your code between `10` and `10.000` times, depending on execution time of first execution, and print the average execution time.
120135

121-
### ➡️ Update readme benchmarks
136+
`cargo time` has three modes of execution:
122137

123-
The template can write benchmark times to the readme via the `cargo time` command.
138+
1. `cargo time` without arguments incrementally benches solutions that do not have been stored in the readme yet and skips the rest.
139+
2. `cargo time <day>` benches a single solution.
140+
3. `cargo time --all` benches all solutions.
124141

125-
By default, this command checks for missing benchmarks, runs those solutions, and then updates the table. If you want to (re-)time all solutions, run `cargo time --all`. If you want to (re-)time one specific solution, run `cargo time <day>`.
142+
By default, `cargo time` does not write to the readme. In order to do so, append the `--store` flag: `cargo time --store`.
126143

127-
Please note that these are not _scientific_ benchmarks, understand them as a fun approximation. 😉 Timings, especially in the microseconds range, might change a bit between invocations.
144+
> Please note that these are not _scientific_ benchmarks, understand them as a fun approximation. 😉 Timings, especially in the microseconds range, might change a bit between invocations.
128145
129146
### ➡️ Run all tests
130147

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod template;
22

33
// Use this file to add helper functions and additional modules.
4-

src/main.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ mod args {
2424
Solve {
2525
day: Day,
2626
release: bool,
27-
time: bool,
2827
dhat: bool,
2928
submit: Option<u8>,
3029
},
3130
All {
3231
release: bool,
33-
time: bool,
3432
},
3533
Time {
3634
all: bool,
3735
day: Option<Day>,
36+
store: bool,
3837
},
3938
#[cfg(feature = "today")]
4039
Today,
@@ -46,14 +45,15 @@ mod args {
4645
let app_args = match args.subcommand()?.as_deref() {
4746
Some("all") => AppArguments::All {
4847
release: args.contains("--release"),
49-
time: args.contains("--time"),
5048
},
5149
Some("time") => {
5250
let all = args.contains("--all");
51+
let store = args.contains("--store");
5352

5453
AppArguments::Time {
5554
all,
5655
day: args.opt_free_from_str()?,
56+
store,
5757
}
5858
}
5959
Some("download") => AppArguments::Download {
@@ -70,7 +70,6 @@ mod args {
7070
day: args.free_from_str()?,
7171
release: args.contains("--release"),
7272
submit: args.opt_value_from_str("--submit")?,
73-
time: args.contains("--time"),
7473
dhat: args.contains("--dhat"),
7574
},
7675
#[cfg(feature = "today")]
@@ -101,8 +100,8 @@ fn main() {
101100
std::process::exit(1);
102101
}
103102
Ok(args) => match args {
104-
AppArguments::All { release, time } => all::handle(release, time),
105-
AppArguments::Time { day, all } => time::handle(day, all),
103+
AppArguments::All { release } => all::handle(release),
104+
AppArguments::Time { day, all, store } => time::handle(day, all, store),
106105
AppArguments::Download { day } => download::handle(day),
107106
AppArguments::Read { day } => read::handle(day),
108107
AppArguments::Scaffold { day, download } => {
@@ -114,10 +113,9 @@ fn main() {
114113
AppArguments::Solve {
115114
day,
116115
release,
117-
time,
118116
dhat,
119117
submit,
120-
} => solve::handle(day, release, time, dhat, submit),
118+
} => solve::handle(day, release, dhat, submit),
121119
#[cfg(feature = "today")]
122120
AppArguments::Today => {
123121
match Day::today() {

src/template/commands/all.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::template::{all_days, run_multi::run_multi};
22

3-
pub fn handle(is_release: bool, is_timed: bool) {
4-
run_multi(&all_days().collect(), is_release, is_timed);
3+
pub fn handle(is_release: bool) {
4+
run_multi(&all_days().collect(), is_release, false);
55
}

src/template/commands/solve.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::process::{Command, Stdio};
22

33
use crate::template::Day;
44

5-
pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Option<u8>) {
5+
pub fn handle(day: Day, release: bool, dhat: bool, submit_part: Option<u8>) {
66
let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()];
77

88
if dhat {
@@ -23,10 +23,6 @@ pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Opti
2323
cmd_args.push(submit_part.to_string());
2424
}
2525

26-
if time {
27-
cmd_args.push("--time".to_string());
28-
}
29-
3026
let mut cmd = Command::new("cargo")
3127
.args(&cmd_args)
3228
.stdout(Stdio::inherit())

src/template/commands/time.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use crate::template::run_multi::run_multi;
44
use crate::template::timings::Timings;
55
use crate::template::{all_days, readme_benchmarks, Day};
66

7-
pub fn handle(day: Option<Day>, recreate_all: bool) {
7+
pub fn handle(day: Option<Day>, run_all: bool, store: bool) {
88
let stored_timings = Timings::read_from_file();
99

1010
let days_to_run = day.map_or_else(
1111
|| {
12-
if recreate_all {
12+
if run_all {
1313
all_days().collect()
1414
} else {
1515
// when the `--all` flag is not set, filter out days that are fully benched.
@@ -23,16 +23,18 @@ pub fn handle(day: Option<Day>, recreate_all: bool) {
2323

2424
let timings = run_multi(&days_to_run, true, true).unwrap();
2525

26-
let merged_timings = stored_timings.merge(&timings);
27-
merged_timings.store_file().unwrap();
26+
if store {
27+
let merged_timings = stored_timings.merge(&timings);
28+
merged_timings.store_file().unwrap();
2829

29-
println!();
30-
match readme_benchmarks::update(merged_timings) {
31-
Ok(()) => {
32-
println!("Stored updated benchmarks.");
33-
}
34-
Err(_) => {
35-
eprintln!("Failed to store updated benchmarks.");
30+
println!();
31+
match readme_benchmarks::update(merged_timings) {
32+
Ok(()) => {
33+
println!("Stored updated benchmarks.");
34+
}
35+
Err(_) => {
36+
eprintln!("Failed to store updated benchmarks.");
37+
}
3638
}
3739
}
3840
}

0 commit comments

Comments
 (0)