added better time measurement

This commit is contained in:
WanderingPenwing 2024-12-04 13:13:55 +01:00
parent f9dda499e2
commit 855472e42a
2 changed files with 33 additions and 22 deletions

View file

@ -70,33 +70,40 @@ impl Solver {
} }
pub fn solve(&mut self, solver_limit: Option<usize>) -> Result<(), WaveError> { pub fn solve(&mut self, solver_limit: Option<usize>) -> Result<(), WaveError> {
let now = Instant::now(); let start_time = Instant::now();
self.display(ui::DisplayMode::Full); self.display(ui::DisplayMode::Full);
let mut propagation_counter: usize = 0; let mut propagation_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO);
let mut collapse_counter: usize = 0; let mut collapse_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO);
let mut backtrack_counter: (usize, std::time::Duration) = (0, std::time::Duration::ZERO);
println!("# started"); println!("# started");
self.debug("--------"); self.debug("--------");
while self.history.len() < self.size * self.size { while self.history.len() < self.size * self.size {
self.debug(&format!("\n## while, h={}/{}", self.last_move_index, self.history.len())); self.debug(&format!("\n## while, h={}/{}", self.last_move_index, self.history.len()));
while self.last_move_index < self.history.len() && self.history.len() < self.size * self.size { while self.last_move_index < self.history.len() && self.history.len() < self.size * self.size {
let mut backtrack = 0; let mut need_backtrack = false;
let propagation_start = Instant::now();
match self.propagate_collapse() { match self.propagate_collapse() {
Ok(_) => {}, Ok(_) => {},
Err(reason) => { Err(reason) => {
if let WaveError::Contradiction = reason { if let WaveError::Contradiction = reason {
backtrack = 1; need_backtrack = true;
} else { } else {
return Err(reason) return Err(reason)
} }
} }
}; };
while backtrack > 0 { propagation_counter.0 += 1;
backtrack -=1; propagation_counter.1 += propagation_start.elapsed();
if need_backtrack {
let backtrack_start = Instant::now();
self.backtrack()?; self.backtrack()?;
backtrack_counter.0 += 1;
backtrack_counter.1 += backtrack_start.elapsed();
} }
propagation_counter += 1;
} }
if self.grid_display { if self.grid_display {
@ -107,21 +114,34 @@ impl Solver {
} }
} }
self.collapse()?; let collapse_start = Instant::now();
collapse_counter += 1; self.collapse()?;
collapse_counter.0 += 1;
collapse_counter.1 += collapse_start.elapsed();
if !self.debug_display && !self.grid_display { if !self.debug_display && !self.grid_display {
self.progress_bar(); self.progress_bar();
} }
if let Some(limit) = solver_limit { if let Some(limit) = solver_limit {
if collapse_counter >= limit { if collapse_counter.0 >= limit {
break; break;
} }
} }
} }
self.debug("--------"); self.debug("--------");
let elapsed = now.elapsed(); let total_elapsed = start_time.elapsed();
println!("\n# finished in {} propagations ({} forced collapse), {:.2?} ({:.2?}/propagation)", propagation_counter, collapse_counter, elapsed, elapsed/(propagation_counter as u32)); let propagation_percentage = (propagation_counter.1.as_secs_f64() / total_elapsed.as_secs_f64()) * 100.0;
let collapse_percentage = (collapse_counter.1.as_secs_f64() / total_elapsed.as_secs_f64()) * 100.0;
let backtrack_percentage = (backtrack_counter.1.as_secs_f64() / total_elapsed.as_secs_f64()) * 100.0;
println!("\n# finished in {:.2?} ({:.2?}% propagation, {:.2?}% forced collapse, {:.2?}% backtrack)",
total_elapsed, propagation_percentage, collapse_percentage, backtrack_percentage
);
println!("- {} propagations ({:.2?}), {} forced collapse ({:.2?}), {} backtrack ({:.2?})",
propagation_counter.0, (propagation_counter.1.checked_div(propagation_counter.0 as u32)).unwrap_or(std::time::Duration::ZERO),
collapse_counter.0, (collapse_counter.1.checked_div(collapse_counter.0 as u32)).unwrap_or(std::time::Duration::ZERO),
backtrack_counter.0, (backtrack_counter.1.checked_div(backtrack_counter.0 as u32)).unwrap_or(std::time::Duration::ZERO)
);
if !self.grid_display || self.debug_display { if !self.grid_display || self.debug_display {
self.display(ui::DisplayMode::Full); self.display(ui::DisplayMode::Full);
} }

9
todo
View file

@ -1,9 +0,0 @@
more check for impossible
cargo run 4 --grid --debug --ask --norand --limit 220
--d-fc-g
error :
when backtracking, always adding the state back is wrong
(maybe it was not there)