use criterion::{criterion_group, criterion_main, Criterion}; type STATE = (usize, usize); fn successors(state: &STATE) -> Vec { let mut states = vec![]; if state.0 > 0 { states.push((state.0 - 1, state.1)); } if state.0 < 8 { states.push((state.0 + 1, state.1)); } if state.1 > 0 { states.push((state.0, state.1 - 1)); } if state.1 < 8 { states.push((state.0, state.1 + 1)); } states } fn heuristic(state: &STATE, goal: &STATE) -> usize { state.0.abs_diff(goal.0) + state.1.abs_diff(goal.1) } fn bfs(init: &STATE, goal: &STATE) -> Option> { searchlib::bfs::solve(init, successors, |s| s == goal) } fn dfs(init: &STATE, goal: &STATE) -> Option> { searchlib::dfs::solve(init, successors, |s| s == goal) } fn gbfs(init: &STATE, goal: &STATE) -> Option> { searchlib::gbfs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn lgbfs(init: &STATE, goal: &STATE) -> Option> { searchlib::lgbfs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn hcs(init: &STATE, goal: &STATE) -> Option> { searchlib::hcs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn ehcs(init: &STATE, goal: &STATE) -> Option> { searchlib::ehcs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn criterion_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("visit_one"); group.bench_function("BFS", |b| b.iter(|| bfs(&(0, 0), &(8, 8)))); group.bench_function("DFS", |b| b.iter(|| dfs(&(0, 0), &(8, 8)))); group.bench_function("GBFS", |b| b.iter(|| gbfs(&(0, 0), &(8, 8)))); group.bench_function("LGBFS", |b| b.iter(|| lgbfs(&(0, 0), &(8, 8)))); group.bench_function("HCS", |b| b.iter(|| hcs(&(0, 0), &(8, 8)))); group.bench_function("EHCS", |b| b.iter(|| ehcs(&(0, 0), &(8, 8)))); group.finish(); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);