Skip to content

Commit

Permalink
Binary: Make line/byte counts optional.
Browse files Browse the repository at this point in the history
Restores performance of read/batches test, and leaves lines() free to
only report the number of lines.
  • Loading branch information
Freaky committed Apr 22, 2018
1 parent 5f5f89b commit c129a8b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ impl Report {
Self { lines, bytes }
}

fn report(&self, name: &str, bytes: u64, lines: u64, elapsed: Duration) {
if bytes != self.bytes {
println!("Warning: expected {} bytes, read {}", self.bytes, bytes);
fn report(&self, name: &str, bytes: Option<u64>, lines: Option<u64>, elapsed: Duration) {
if let Some(bytes) = bytes {
if bytes != self.bytes {
println!("Warning: expected {} bytes, read {}", self.bytes, bytes);
}
}

if lines != self.lines {
println!("Warning: expected {} lines, read {}", self.lines, lines);
if let Some(lines) = lines {
if lines != self.lines {
println!("Warning: expected {} lines, read {}", self.lines, lines);
}
}

let elapsed =
Expand All @@ -68,18 +72,16 @@ fn try_baseline(report: &Report, filename: &str) {

let start = Instant::now();
let mut bytes = 0_u64;
let mut lines = 0_u64;

let mut buf = [0; BUFFER_SIZE];
while let Ok(r) = infile.read(&mut buf[..]) {
if r == 0 {
break;
}
bytes += r as u64;
lines += Memchr::new(b'\n', &buf[..r]).count() as u64;
}

report.report("read() 1 MiB", bytes, lines, start.elapsed());
report.report("read() 1 MiB", Some(bytes), None, start.elapsed());
}

fn try_linereader_batch(report: &Report, filename: &str) {
Expand All @@ -88,15 +90,13 @@ fn try_linereader_batch(report: &Report, filename: &str) {
let mut reader = LineReader::with_capacity(BUFFER_SIZE, infile);

let start = Instant::now();
let mut lines = 0_u64;
let mut bytes = 0_u64;
while let Some(batch) = reader.next_batch() {
let batch = batch.unwrap();
bytes += batch.len() as u64;
lines += Memchr::new(b'\n', batch).count() as u64;
}

report.report("LR::next_batch()", bytes, lines, start.elapsed());
report.report("LR::next_batch()", Some(bytes), None, start.elapsed());
}

fn try_linereader(report: &Report, filename: &str) {
Expand All @@ -112,7 +112,7 @@ fn try_linereader(report: &Report, filename: &str) {
lines += 1;
}

report.report("LR::next_line()", bytes, lines, start.elapsed());
report.report("LR::next_line()", Some(bytes), Some(lines), start.elapsed());
}

fn try_read_until(report: &Report, filename: &str) {
Expand All @@ -129,7 +129,7 @@ fn try_read_until(report: &Report, filename: &str) {
line.clear();
}

report.report("read_until()", bytes, lines, start.elapsed());
report.report("read_until()", Some(bytes), Some(lines), start.elapsed());
}

fn try_read_line(report: &Report, filename: &str) {
Expand All @@ -146,7 +146,7 @@ fn try_read_line(report: &Report, filename: &str) {
line.clear();
}

report.report("read_line()", bytes, lines, start.elapsed());
report.report("read_line()", Some(bytes), Some(lines), start.elapsed());
}

fn try_lines_iter(report: &Report, filename: &str) {
Expand All @@ -155,13 +155,11 @@ fn try_lines_iter(report: &Report, filename: &str) {

let start = Instant::now();
let mut lines = 0_u64;
let mut bytes = 0_u64;
for line in infile.lines() {
bytes += line.unwrap().len() as u64;
for _line in infile.lines() {
lines += 1;
}

report.report("lines()", bytes, lines, start.elapsed());
report.report("lines()", None, Some(lines), start.elapsed());
}

fn main() {
Expand Down

0 comments on commit c129a8b

Please sign in to comment.