use std::{env, fs::File, io::Write, time}; fn write_buf_un(w: &mut impl Write) -> Result<(), std::io::Error> { let buf: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; w.write_all(&buf)?; Ok(()) } #[inline] fn write_buf(w: &mut impl Write) -> Result<(), std::io::Error> { let mut buf = [0u8; 10]; buf[0] = 0; buf[1] = 1; buf[2] = 2; buf[3] = 3; buf[4] = 4; buf[5] = 5; buf[6] = 6; buf[7] = 7; buf[8] = 8; buf[9] = 9; w.write_all(&buf)?; Ok(()) } fn write_many(w: &mut impl Write) -> Result<(), std::io::Error> { w.write_all(&[0])?; w.write_all(&[1])?; w.write_all(&[2])?; w.write_all(&[3])?; w.write_all(&[4])?; w.write_all(&[5])?; w.write_all(&[6])?; w.write_all(&[7])?; w.write_all(&[8])?; w.write_all(&[9])?; Ok(()) } fn main() { let args: Vec = env::args().collect(); match args[1].as_ref() { "1" => { _ = std::fs::remove_file("/tmp/write_buf_1"); let mut file = File::create("/tmp/write_buf_1").unwrap(); let now = time::Instant::now(); while now.elapsed().as_secs() < 1u64 { write_buf(&mut file).expect("written"); } } "2" => { _ = std::fs::remove_file("/tmp/write_many_1"); let mut file = File::create("/tmp/write_many_1").unwrap(); let now = time::Instant::now(); while now.elapsed().as_secs() < 1u64 { write_many(&mut file).expect("written"); } } "3" => { _ = std::fs::remove_file("/tmp/write_many_buf_1"); let file = File::create("/tmp/write_many_buf_1").unwrap(); let mut w = std::io::BufWriter::new(file); let now = time::Instant::now(); while now.elapsed().as_secs() < 1u64 { write_many(&mut w).expect("written"); } } "4" => { _ = std::fs::remove_file("/tmp/write_buf_buf_1"); let file = File::create("/tmp/write_buf_buf_1").unwrap(); let mut w = std::io::BufWriter::new(file); let now = time::Instant::now(); while now.elapsed().as_secs() < 1u64 { write_buf(&mut w).expect("written"); } } "5" => { _ = std::fs::remove_file("/tmp/write_buf_buf_un_1"); let file = File::create("/tmp/write_buf_buf_un_1").unwrap(); let mut w = std::io::BufWriter::new(file); let now = time::Instant::now(); while now.elapsed().as_secs() < 1u64 { write_buf_un(&mut w).expect("written"); } } _ => println!("expect 1,2,3,1"), } }