Timings misc (#1945)

This commit is contained in:
John Kerl 2026-02-01 11:56:13 -05:00 committed by GitHub
parent 96f13b6618
commit e94ae5a87d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 137 additions and 0 deletions

View file

@ -0,0 +1,14 @@
//use std::fs::File;
use std::io::{self, prelude::*, BufReader};
fn main() -> io::Result<()> {
//let file = File::open("foo.txt")?;
let file = io::stdin();
let reader = BufReader::new(file);
for line in reader.lines() {
println!("{}", line?);
}
Ok(())
}

View file

@ -0,0 +1,24 @@
// Claude
use std::fs::File;
use std::io::{self, BufRead, BufReader};
// Method 1: Using BufReader with lines() - Good balance of speed and memory
fn read_lines_buf_reader(path: &str) -> io::Result<()> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut n = 0;
for line in reader.lines() {
let _line = line?;
// Process line here
n += 1;
}
println!("line count {}", n);
Ok(())
}
fn main() {
let _ = read_lines_buf_reader("/Users/kerl/tmp/big");
}

View file

@ -0,0 +1,21 @@
// Claude
use std::fs::File;
use std::io::{self, BufRead, BufReader};
// Method 2: Using BufReader with read_line - More control, similar performance
fn read_lines_manual(path: &str) -> io::Result<()> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let mut line = String::new();
while reader.read_line(&mut line)? > 0 {
// Process line here
line.clear(); // Reuse the string buffer
}
Ok(())
}
fn main() {
let _ = read_lines_manual("/Users/kerl/tmp/big");
}

View file

@ -0,0 +1,21 @@
// Claude
use std::fs::File;
use std::io::{self, BufRead, BufReader};
// Method 3: Using custom buffer size for larger files
fn read_lines_custom_buffer(path: &str) -> io::Result<()> {
const BUFFER_SIZE: usize = 128 * 1024; // 128KB buffer
let file = File::open(path)?;
let reader = BufReader::with_capacity(BUFFER_SIZE, file);
for line in reader.lines() {
let _line = line?;
// Process line here
}
Ok(())
}
fn main() {
let _ = read_lines_custom_buffer("/Users/kerl/tmp/big");
}

View file

@ -0,0 +1,24 @@
// Claude
use std::fs::File;
use std::io::{self, BufRead, BufReader};
// Method 4: Memory mapped files for very large files
fn read_lines_memmap(path: &str) -> io::Result<()> {
use memmap2::Mmap;
let file = File::open(path)?;
let mmap = unsafe { Mmap::map(&file)? };
// Convert memory map to string and iterate over lines
if let Ok(contents) = std::str::from_utf8(&mmap) {
for line in contents.lines() {
// Process line here
}
}
Ok(())
}
fn main() {
let _ = read_lines_custom_buffer("/Users/kerl/tmp/big");
}

View file

@ -0,0 +1,9 @@
justtime ./catc < ~/tmp/big > /dev/null
justtime ./catc0 < ~/tmp/big > /dev/null
justtime ./catm < ~/tmp/big > /dev/null
justtime ./catrust < ~/tmp/big > /dev/null
justtime ./catrust2 < ~/tmp/big > /dev/null
justtime ./ff1 < ~/tmp/big > /dev/null
justtime ./ff2 < ~/tmp/big > /dev/null
justtime ./ff3 < ~/tmp/big > /dev/null
justtime ./catgo < ~/tmp/big > /dev/null

View file

@ -0,0 +1,24 @@
bash ./new-timings.sh
TIME IN SECONDS 9.691 -- ./catc
TIME IN SECONDS 8.137 -- ./catc0
TIME IN SECONDS 0.005 -- ./catm
TIME IN SECONDS 12.975 -- ./catrust
TIME IN SECONDS 52.282 -- ./catrust2
TIME IN SECONDS 7.488 -- ./ff1
TIME IN SECONDS 4.646 -- ./ff2
TIME IN SECONDS 6.932 -- ./ff3
TIME IN SECONDS 6.412 -- ./catgo
TIME IN SECONDS 0.005 -- ./catm
TIME IN SECONDS 4.646 -- ./ff2
TIME IN SECONDS 6.412 -- ./catgo
TIME IN SECONDS 6.932 -- ./ff3
TIME IN SECONDS 7.488 -- ./ff1
TIME IN SECONDS 8.137 -- ./catc0
TIME IN SECONDS 9.691 -- ./catc
TIME IN SECONDS 12.975 -- ./catrust
TIME IN SECONDS 52.282 -- ./catrust2
$ justtime mlr cat ~/tmp/big > /dev/null
TIME IN SECONDS 79.106 -- mlr cat /Users/kerl/tmp/big