#![feature(test)] extern crate kg_diag; extern crate test; use test::Bencher; use kg_diag::*; fn make_string() -> String { let mut s = String::with_capacity((1024 * 1024 * 81) / 80); for i in 0u64..1024 * 1024 { let c: u8 = ((i % (128 - 32)) + 32) as u8; s.push(c as char); if i % 80 == 0 { s.push('\n'); } } s } fn dyn_scan(r: &mut dyn CharReader) -> ParseResult { let mut count = 0; while let Some(c) = r.next_char()? { if c == '0' { count += 1; } } Ok(count) } fn static_scan(r: &mut R) -> ParseResult { let mut count = 0; while let Some(c) = r.next_char()? { if c == '0' { count += 1; } } Ok(count) } #[bench] fn dyn_dispatch(b: &mut Bencher) { let s = make_string(); let mut reader = MemCharReader::new(s.as_bytes()); b.iter(|| dyn_scan(&mut reader)); } #[bench] fn static_dispatch(b: &mut Bencher) { let s = make_string(); let mut reader = MemCharReader::new(s.as_bytes()); b.iter(|| static_scan(&mut reader)); }