use calliper::utils::black_box; use calliper::{Runner, Scenario}; #[inline(never)] #[no_mangle] fn binary_search_impl(haystack: &[u8], needle: u8) -> Result { haystack.binary_search(&needle) } fn bench_binary_search() { let range = (0..255).collect::>(); let _ = black_box(binary_search_impl(black_box(&range), black_box(253))); } #[inline(never)] #[no_mangle] fn linear_search_impl(haystack: &[u8], needle: u8) -> Option { haystack.iter().position(|n| *n == needle) } fn bench_linear_search() { let range = (0..255).collect::>(); black_box(linear_search_impl(black_box(&range), black_box(253))); } fn main() -> Result<(), Box> { let runner = Runner::default(); let benches = [ Scenario::new(bench_linear_search), Scenario::new(bench_binary_search), ]; if let Some(results) = runner.run(&benches)? { for res in results.into_iter() { println!("{}", res.parse()); } } Ok(()) }