use std::thread::sleep;
use std::time::{Duration, Instant};
use readahead_iterator::Readahead;
const PROCESS_DELAY: Duration = Duration::from_millis(200);
const GENERATE_DELAY: Duration = Duration::from_millis(500);
const N: usize = 20;
fn slow_iterator() -> impl Iterator<Item = usize> {
(0..N).inspect(|i| {
println!("generating {}...", i);
sleep(GENERATE_DELAY);
})
}
pub fn main() {
let start = Instant::now();
println!("without readahead:");
for i in slow_iterator() {
println!("processing {}...", i);
sleep(PROCESS_DELAY);
}
println!("elapsed: {:?}", Instant::now() - start);
let start = Instant::now();
println!("with readahead:");
for i in Readahead::new(slow_iterator(), 10) {
println!("processing {}...", i);
sleep(PROCESS_DELAY);
}
println!("elapsed: {:?}", Instant::now() - start);
}