From ee488f73f3ce78d436e8634b78a0b425e88fede0 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 8 Feb 2023 01:58:10 -0300 Subject: [PATCH 1/2] initial implementation for benchmark --- src/algorithms/mod.rs | 7 ++++++- src/ui/buttons/mod.rs | 10 ++++++++++ src/ui/mod.rs | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/algorithms/mod.rs b/src/algorithms/mod.rs index f6f8cea..385e728 100644 --- a/src/algorithms/mod.rs +++ b/src/algorithms/mod.rs @@ -1,3 +1,5 @@ +use std::time::Instant; + pub mod bubble_sort; pub mod insertion_sort; pub mod selection_sort; @@ -20,13 +22,16 @@ pub trait Sorter { fn get_state(&self) -> (usize, usize); /// Loops all states and reset state. - fn run(&mut self, array: &mut Vec) { + /// Returns time elapsed (microsseconds). + fn run(&mut self, array: &mut Vec) -> u128 { + let now = Instant::now(); loop { if self.step(array) { break; } } self.reset_state(); + now.elapsed().as_micros() } /// Takes a single step in running the algorithm. diff --git a/src/ui/buttons/mod.rs b/src/ui/buttons/mod.rs index 3472a4f..a727cbd 100644 --- a/src/ui/buttons/mod.rs +++ b/src/ui/buttons/mod.rs @@ -29,6 +29,16 @@ impl ButtonHandler { app.numbers = util::gen_random_vector(FLOOR, CEIL, VECTOR_SIZE); app.original_numbers = app.numbers.clone(); } + + pub(super) fn handle_benchmark(app: &mut Visualizer) { + app.reset(); + for (i, option) in Algorithms::iter().enumerate() { + app.selected = option; + app.switch_algorithm(); + let time = app.sorter.run(&mut app.numbers); + app.bench_results[i] = time; + } + } } #[cfg(test)] diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 22b4313..6ca6c86 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -49,6 +49,8 @@ pub(crate) struct Visualizer<'a> { original_numbers: Vec, state: State, sorter: Box, + bench: bool, + bench_results: Vec, } impl<'a> Default for Visualizer<'a> { @@ -60,6 +62,8 @@ impl<'a> Default for Visualizer<'a> { state: State::Start, original_numbers: numbers, sorter: Box::new(BubbleSort::new()), + bench: false, + bench_results: (0..3).collect::>(), } } } @@ -117,6 +121,17 @@ impl Visualizer<'_> { }); } + fn draw_bench(&self, ui: &mut Ui) { + if self.bench { + ui.horizontal(|ui| { + ui.add_space(400.); + for (i, option) in Algorithms::iter().enumerate() { + ui.label(format!("{option:?} {}", self.bench_results[i])); + } + }); + } + } + /// Create the ComboBox and return true if algorithm selection has been changed. fn handle_combox_box(&mut self, ui: &mut Ui) -> bool { let previous_selection: Algorithms = self.selected; @@ -162,6 +177,10 @@ impl Visualizer<'_> { if ui.add(Button::new("Shuffle")).clicked() { ButtonHandler::handle_shuffle(self); } + if ui.add(Button::new("Benchmark")).clicked() { + ButtonHandler::handle_benchmark(self); + self.bench = true + } } /// If running, take a step and sleep for WAIT_TIME. @@ -224,6 +243,7 @@ impl eframe::App for Visualizer<'_> { ui.add_space(PADDING); self.draw_numbers(ui); + self.draw_bench(ui); }); } } From 45c694e0667388f386de1748cbe701cc8ed2deb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Felipe=20Ramos=20Ferreira?= Date: Sat, 11 Feb 2023 15:04:29 -0300 Subject: [PATCH 2/2] [NEW] inicializando benchmark --- src/ui/buttons/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ui/buttons/mod.rs b/src/ui/buttons/mod.rs index a727cbd..6ac72cf 100644 --- a/src/ui/buttons/mod.rs +++ b/src/ui/buttons/mod.rs @@ -37,6 +37,7 @@ impl ButtonHandler { app.switch_algorithm(); let time = app.sorter.run(&mut app.numbers); app.bench_results[i] = time; + println!("{}", app.bench_results[i]); } } }