perf: avoid clone in bencher (#67)
--------- Co-authored-by: Matt Clarke <mattjclarke94@gmail.com>
This commit is contained in:
parent
3abf88c16a
commit
a50047ab0d
1 changed files with 4 additions and 8 deletions
|
@ -9,7 +9,7 @@ use std::{cmp, env, process};
|
||||||
use crate::template::ANSI_BOLD;
|
use crate::template::ANSI_BOLD;
|
||||||
use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET};
|
use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET};
|
||||||
|
|
||||||
pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
|
pub fn run_part<I: Copy, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
|
||||||
let part_str = format!("Part {part}");
|
let part_str = format!("Part {part}");
|
||||||
|
|
||||||
let (result, duration, samples) =
|
let (result, duration, samples) =
|
||||||
|
@ -25,15 +25,13 @@ pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, d
|
||||||
/// Run a solution part. The behavior differs depending on whether we are running a release or debug build:
|
/// Run a solution part. The behavior differs depending on whether we are running a release or debug build:
|
||||||
/// 1. in debug, the function is executed once.
|
/// 1. in debug, the function is executed once.
|
||||||
/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.)
|
/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.)
|
||||||
fn run_timed<I: Clone, T>(
|
fn run_timed<I: Copy, T>(
|
||||||
func: impl Fn(I) -> T,
|
func: impl Fn(I) -> T,
|
||||||
input: I,
|
input: I,
|
||||||
hook: impl Fn(&T),
|
hook: impl Fn(&T),
|
||||||
) -> (T, Duration, u128) {
|
) -> (T, Duration, u128) {
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
let result = {
|
let result = {
|
||||||
let input = input.clone();
|
|
||||||
|
|
||||||
#[cfg(feature = "dhat-heap")]
|
#[cfg(feature = "dhat-heap")]
|
||||||
let _profiler = dhat::Profiler::new_heap();
|
let _profiler = dhat::Profiler::new_heap();
|
||||||
|
|
||||||
|
@ -52,7 +50,7 @@ fn run_timed<I: Clone, T>(
|
||||||
(result, run.0, run.1)
|
(result, run.0, run.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench<I: Clone, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
|
fn bench<I: Copy, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
|
||||||
let mut stdout = stdout();
|
let mut stdout = stdout();
|
||||||
|
|
||||||
print!(" > {ANSI_ITALIC}benching{ANSI_RESET}");
|
print!(" > {ANSI_ITALIC}benching{ANSI_RESET}");
|
||||||
|
@ -64,10 +62,8 @@ fn bench<I: Clone, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) ->
|
||||||
let mut timers: Vec<Duration> = vec![];
|
let mut timers: Vec<Duration> = vec![];
|
||||||
|
|
||||||
for _ in 0..bench_iterations {
|
for _ in 0..bench_iterations {
|
||||||
// need a clone here to make the borrow checker happy.
|
|
||||||
let cloned = input.clone();
|
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
black_box(func(black_box(cloned)));
|
black_box(func(black_box(input)));
|
||||||
timers.push(timer.elapsed());
|
timers.push(timer.elapsed());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue