From 3010331c2e4f46d02ca053dd47d62acbd54da311 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 20 Oct 2024 06:57:16 +0100 Subject: [PATCH] reduce code duplication --- benches/bench_dubins.rs | 42 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/benches/bench_dubins.rs b/benches/bench_dubins.rs index 4b87b73..c91f9fd 100644 --- a/benches/bench_dubins.rs +++ b/benches/bench_dubins.rs @@ -3,43 +3,32 @@ use dubins_paths::{DubinsPath, PathType, PosRot}; const TURN_RADIUS: f32 = 1. / 0.00076; -fn bench_shortest_csc_path(c: &mut Criterion) { +fn setup_benchmark() -> (PosRot, PosRot) { let q0: PosRot = [2000., 2000., 0.].into(); let q1: PosRot = [0., 0., std::f32::consts::PI].into(); + (q0, q1) +} - c.bench_function("shortest_csc_path", |b| { +fn bench_shortest_path_type(c: &mut Criterion, name: &str, path_types: &[PathType]) { + let (q0, q1) = setup_benchmark(); + + c.bench_function(name, |b| { b.iter(|| { - DubinsPath::shortest_in( - black_box(q0), - black_box(q1), - black_box(TURN_RADIUS), - black_box(&PathType::CSC), - ) - .unwrap() + DubinsPath::shortest_in(black_box(q0), black_box(q1), black_box(TURN_RADIUS), black_box(path_types)).unwrap() }) }); } -fn bench_shortest_ccc_path(c: &mut Criterion) { - let q0: PosRot = [2000., 2000., 0.].into(); - let q1: PosRot = [0., 0., std::f32::consts::PI].into(); +fn bench_shortest_csc_path(c: &mut Criterion) { + bench_shortest_path_type(c, "shortest_csc_path", &PathType::CSC); +} - c.bench_function("shortest_ccc_path", |b| { - b.iter(|| { - DubinsPath::shortest_in( - black_box(q0), - black_box(q1), - black_box(TURN_RADIUS), - black_box(&PathType::CCC), - ) - .unwrap() - }) - }); +fn bench_shortest_ccc_path(c: &mut Criterion) { + bench_shortest_path_type(c, "shortest_ccc_path", &PathType::CCC); } fn bench_shortest_path(c: &mut Criterion) { - let q0: PosRot = [2000., 2000., 0.].into(); - let q1: PosRot = [0., 0., std::f32::consts::PI].into(); + let (q0, q1) = setup_benchmark(); c.bench_function("shortest_path", |b| { b.iter(|| DubinsPath::shortest_from(black_box(q0), black_box(q1), black_box(TURN_RADIUS)).unwrap()) @@ -48,8 +37,7 @@ fn bench_shortest_path(c: &mut Criterion) { fn bench_many_sample(c: &mut Criterion) { const STEP_DISTANCE: f32 = 10.; - let q0: PosRot = [2000., 2000., 0.].into(); - let q1: PosRot = [0., 0., std::f32::consts::PI].into(); + let (q0, q1) = setup_benchmark(); let path = DubinsPath::shortest_from(q0, q1, TURN_RADIUS).unwrap(); c.bench_function("many_sample", |b| b.iter(|| path.sample_many(black_box(STEP_DISTANCE))));