Skip to content

Instantly share code, notes, and snippets.

@grahamking
Last active August 9, 2021 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamking/a1bd00581fd15908338ee65f7937cbf1 to your computer and use it in GitHub Desktop.
Save grahamking/a1bd00581fd15908338ee65f7937cbf1 to your computer and use it in GitHub Desktop.
use std::error::Error;
use std::io::{stdin, Read};
use std::time;
fn main() -> Result<(), Box<dyn Error>> {
let mut v: Vec<u8> = vec![0; 65536];
let mut howmany = 0;
let s = stdin();
let mut cin = s.lock();
let mut n = 1;
let before = time::Instant::now();
while n > 0 {
n = cin.read(&mut v).unwrap();
howmany += n;
}
let elapsed = before.elapsed();
let giga = howmany as f64 / 1_000_000_000f64;
println!("read {} bytes in {:?}", howmany, elapsed);
let speed = giga / elapsed.as_secs() as f64;
println!("{} GB/s", speed);
Ok(())
}
use std::io::{Write, stdout};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let v: Vec<u8> = vec![0; 65536];
let s = stdout();
let mut out = s.lock();
let start = std::time::Instant::now();
while start.elapsed().as_secs() < 5 {
out.write_all(&v)?;
}
Ok(())
}
@grahamking
Copy link
Author

grahamking commented Aug 4, 2021

Daniel Lemire's blog post "How fast can you pipe a large file to a C++ program?" inspired me want to try it in Rust, so here it is.

I changed the buffer size to 65k to match my Linux buffer size. I get about 5 or 6 GB/s. The bottleneck seems to be the reader. I can get over 7 GB/s piping the writer straight into pv, and not using the reader.

It runs a little faster if I put pv between writer and reader, which is interesting. Not sure why that is yet.

Using the C and C++ versions in the author's benchmark I get 4 or 5 GB/s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment