benchmark progress bar

This commit is contained in:
WanderingPenwing 2024-12-05 22:25:28 +01:00
parent 9dd739ffcc
commit 78fc4bc205
3 changed files with 65 additions and 26 deletions

View file

@ -114,7 +114,7 @@ pub struct Solver {
square_size: usize,
debug_display: bool,
grid_display: bool,
benchmarking: bool,
solve_display: bool,
collapse_option: cell::CollapseOption,
}
@ -131,7 +131,7 @@ impl Solver {
square_size: order,
debug_display: false,
grid_display: false,
benchmarking: false,
solve_display: true,
collapse_option: cell::CollapseOption::Random,
}
}
@ -153,30 +153,58 @@ impl Solver {
}
pub fn benchmark(&mut self, solve_number: usize) {
self.benchmarking = true;
self.solve_display = false;
println!();
if self.grid_display {
self.display(ui::DisplayMode::Full);
}
let mut results_sum: SolveResult = SolveResult::default();
let mut error_number: usize = 0;
for _ in 0..solve_number {
println!();
for index in 0..solve_number {
self.reset_grid();
match self.solve(None) {
Ok(result) => {
if self.debug_display {
println!("\n{}", result);
}
results_sum += result;
}
Err(reason) => {
if self.debug_display {
println!("{}", reason);
}
error_number += 1;
}
}
if !self.debug_display && !self.grid_display {
let progress = (index + 1) as f32 / solve_number as f32;
self.progress_bar(progress);
//print!("\r- {} ({}/{})", progress, index + 1, solve_number);
}
if self.grid_display {
if !self.debug_display {
self.display(ui::DisplayMode::Erase);
} else {
self.display(ui::DisplayMode::Full);
}
}
}
if self.debug_display {
println!("\n---------");
} else {
println!();
}
results_sum *= 1.0 / ((solve_number - error_number) as f32);
println!("\n---------\n{}\n\n{} errors", results_sum, error_number);
println!("\nbenchmark {} (n = {})\n{}\n({} errors)", self.square_size, solve_number, results_sum, error_number);
self.solve_display = true;
}
pub fn solve(&mut self, solver_limit: Option<usize>) -> Result<SolveResult, WaveError> {
let start_time = Instant::now();
if self.solve_display {
self.display(ui::DisplayMode::Full);
}
let mut propagation_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO);
let mut collapse_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO);
@ -210,7 +238,7 @@ impl Solver {
}
}
if self.grid_display {
if self.grid_display && self.solve_display {
if !self.debug_display {
self.display(ui::DisplayMode::Erase);
} else {
@ -223,8 +251,8 @@ impl Solver {
collapse_counter.0 += 1;
collapse_counter.1 += collapse_start.elapsed();
if !self.debug_display && !self.grid_display {
self.progress_bar();
if !self.debug_display && !self.grid_display && self.solve_display {
self.progress_bar(self.history.len() as f32/((self.size*self.size) as f32));
}
if let Some(limit) = solver_limit {
if collapse_counter.0 >= limit {
@ -233,7 +261,9 @@ impl Solver {
}
}
self.debug("--------");
if !self.grid_display || self.debug_display {
if (!self.grid_display || self.debug_display) && self.solve_display {
println!();
self.display(ui::DisplayMode::Full);
}
let total_time = start_time.elapsed();
@ -243,7 +273,9 @@ impl Solver {
collapse : collapse_counter,
backtrack : backtrack_counter,
};
if self.solve_display {
println!("\n{}", result);
}
Ok(result)
}

View file

@ -1,4 +1,5 @@
use text_io::read;
use std::io::{self, Write};
use super::Solver;
use super::cell;
@ -21,9 +22,6 @@ pub enum DisplayMode {
impl Solver {
pub fn display(&self, display_mode: DisplayMode) {
if self.benchmarking {
return
}
if let DisplayMode::Erase = display_mode {
let height = self.size + self.square_size + 2;
print!("\x1b[{}A", height);
@ -104,11 +102,15 @@ impl Solver {
Ok(())
}
pub fn progress_bar(&self) {
let bar_size = (self.size + self.square_size - 1)*2 + 1;
let progress = self.history.len()*bar_size/(self.size*self.size);
let to_do = bar_size - progress;
print!("\r[{}{}]", "#".repeat(progress), "-".repeat(to_do));
pub fn progress_bar(&self, progress: f32) {
let progress = progress.clamp(0.0, 1.0);
let bar_size = ((self.size + self.square_size - 1)*2 + 1).clamp(10, 100);
let done = (bar_size as f32 * progress).round() as usize;
let to_do = bar_size - done;
print!("\r[{}{}] ({:.2?}%)", "#".repeat(done), "-".repeat(to_do), progress * 100.0);
if let Err(e) = io::stdout().flush() {
eprintln!("Error flushing stdout: {}", e);
}
}
pub fn debug_mode(&mut self, debug_display: bool) {
@ -120,7 +122,7 @@ impl Solver {
}
pub fn debug(&self, text: &str) {
if self.debug_display {
if self.debug_display && self.solve_display {
println!("{}", text);
}
}

11
todo
View file

@ -1,7 +1,12 @@
banchmark 2 (n = 80000)
# total time : 451.66µs (80.53% propagation, 18.73% forced collapse, 0.00% backtrack)
- 13 propagations (27.98µs), 8 forced collapse (10.58µs), 0 backtrack (0.00ns)
(0 errors)
benchmark 3 (n = 3000)
# total time : 10.05ms (62.03% propagation, 37.31% forced collapse, 0.12% backtrack)
- 78 propagations (79.96µs), 48 forced collapse (78.16µs), 0 backtrack (0.00ns)
benchmark 3 (n = 8000)
# total time : 9.94ms (62.15% propagation, 37.50% forced collapse, 0.12% backtrack)
- 78 propagations (79.17µs), 48 forced collapse (77.63µs), 0 backtrack (0.00ns)
(0 errors)
benchmark 4 (n = 300)
# total time : 116.36ms (43.53% propagation, 56.03% forced collapse, 0.23% backtrack)