File I/O performance pitfalls

9898287 relay.public.adnan at outlook.com
Sat Oct 26 01:27:18 UTC 2019


Hi I want to find out what's causing my file writes to be so 
slow. I'm setting up buffer and locking the file and writing them 
to the file.

$ cat d.d && ldc2 -O5  d.d && time ./d >> /dev/null
void main() {
     import std.stdio;
     stdout.setvbuf(4096);
     stdout.lock();

     foreach(i; 0 .. 900_000_000)
         writef("Hello, {}\n", i);
}
real    0m58.790s
user    0m58.545s
sys     0m0.232s

For comparison, I come from the Rust land and here's somewhat 
equivalent code which is consistently faster:

$ cat rust.rs && rustc -C opt-level=3 rust.rs && time ./rust >> 
/dev/null
use std::io::Write;

fn main() {
     let stdout = std::io::stdout();
     let lock = stdout.lock();
     let mut buf = std::io::BufWriter::new(lock);
     for i in 0 .. 900_000_000 {
         buf.write_fmt(format_args!(
             "Hello, {}\n", i
         )).unwrap();
     }
}

real    0m46.502s
user    0m46.263s
sys     0m0.228s

What am I missing?


More information about the Digitalmars-d-learn mailing list