Skip to content

Commit

Permalink
Add a benchmark to avoid file I/O.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Oct 12, 2024
1 parent 51e3343 commit 88880f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 28 additions & 2 deletions benches/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@ use criterion::{criterion_group, criterion_main, Criterion};
use iof::{unwrap, InputStream, ReadInto, ReadOneInto};
use std::{
fs::{read_to_string, File},
io::{BufRead, BufReader, Cursor},
io::{self, BufRead, BufReader, Cursor, Read, Write},
};

#[derive(Clone)]
struct LazyWriter(std::ops::Range<i32>);

impl Read for LazyWriter {
fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
if let Some(num) = self.0.next() {
let s = format!("{} ", num);
let len = s.len();
buf.write_all(s.as_bytes())?;
Ok(len)
} else {
Ok(0)
}
}
}

fn template<R: BufRead>(
case: &'static str,
count: usize,
Expand Down Expand Up @@ -112,5 +128,15 @@ fn file(c: &mut Criterion) {
}))(c);
}

criterion_group!(benches, cursor, file);
fn lazy(c: &mut Criterion) {
let writer = LazyWriter(0..COUNT as i32);
{
(template("lazy - long", COUNT, || BufReader::new(writer.clone())))(c);
}
{
(template("lazy - short", COUNT, || BufReader::new(writer.clone())))(c);
}
}

criterion_group!(benches, cursor, file, lazy);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions examples/benchmark_integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use iof::{sep_by, show, unwrap};
use std::{fs::File, ops::Range, path::PathBuf};

fn main() {
const INTEGERS: [Range<i64>; 3] = [
-0x10000i64..0x10000i64,
const INTEGERS: [Range<i32>; 3] = [
-0x10000..0x10000,
-0x80000000..-0x7fff0000,
0x70000000..0x70010000,
];
Expand Down

0 comments on commit 88880f3

Please sign in to comment.