Skip to content

feat(ui): benchmark #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/algorithms/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::time::Instant;
pub mod bogo_sort;
pub mod bubble_sort;
mod constants;
Expand All @@ -21,13 +22,16 @@ pub trait Sorter {
fn reason(&self) -> Reasons;

/// Loops all states and reset state.
fn run(&mut self, array: &mut Vec<usize>) {
/// Returns time elapsed (microsseconds).
fn run(&mut self, array: &mut Vec<usize>) -> 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.
Expand Down
11 changes: 11 additions & 0 deletions src/ui/buttons/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ 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;
println!("{}", app.bench_results[i]);
}
}
}

#[cfg(test)]
Expand Down
20 changes: 20 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub(crate) struct Visualizer<'a> {
original_numbers: Vec<usize>,
state: State,
sorter: Box<dyn Sorter + 'a>,
bench: bool,
bench_results: Vec<u128>,
}

impl<'a> Default for Visualizer<'a> {
Expand All @@ -61,6 +63,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::<Vec<u128>>(),
}
}
}
Expand Down Expand Up @@ -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_combo_box(&mut self, ui: &mut Ui) -> bool {
let previous_selection: Algorithms = self.selected;
Expand Down Expand Up @@ -165,6 +180,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.
Expand Down Expand Up @@ -226,6 +245,7 @@ impl eframe::App for Visualizer<'_> {

ui.add_space(PADDING);
self.draw_numbers(ui);
self.draw_bench(ui);
});
}
}