Last active
August 9, 2021 19:46
-
-
Save grahamking/a1bd00581fd15908338ee65f7937cbf1 to your computer and use it in GitHub Desktop.
Rust version of https://lemire.me/blog/2021/08/03/how-fast-can-you-pipe-a-large-file-to-a-c-program/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.