1use std::thread::sleep;
17use std::time::{Duration, Instant};
18
19use readahead_iterator::Readahead;
20
21const PROCESS_DELAY: Duration = Duration::from_millis(200);
22const GENERATE_DELAY: Duration = Duration::from_millis(500);
23const N: usize = 20;
24
25fn slow_iterator() -> impl Iterator<Item = usize> {
26 (0..N).inspect(|i| {
27 println!("generating {}...", i);
28 sleep(GENERATE_DELAY);
29 })
30}
31
32pub fn main() {
33 let start = Instant::now();
34 println!("without readahead:");
35 for i in slow_iterator() {
36 println!("processing {}...", i);
37 sleep(PROCESS_DELAY);
38 }
39 println!("elapsed: {:?}", Instant::now() - start);
40
41 let start = Instant::now();
42 println!("with readahead:");
43 for i in Readahead::new(slow_iterator(), 10) {
44 println!("processing {}...", i);
45 sleep(PROCESS_DELAY);
46 }
47 println!("elapsed: {:?}", Instant::now() - start);
48}