File I/O performance pitfalls

rikki cattermole rikki at cattermole.co.nz
Sat Oct 26 02:42:04 UTC 2019


On 26/10/2019 2:27 PM, 9898287 wrote:
> 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

You probably want -O3 not -O5.

> void main() {
>      import std.stdio;
>      stdout.setvbuf(4096);
>      stdout.lock();
> 
>      foreach(i; 0 .. 900_000_000)
>          writef("Hello, {}\n", i);

I assume what you intended is:
writeln("Hello, ", i);

Also for format functions in D you should prefer the templated variation 
as it provides compile time verification of arguments e.g.

writef!"Hello, {}\n%d"(i);

Please note that {} is not a format specifier.
printf style functions (which is what Phobos uses as the basis) for 
format specifiers begin with a percentage sign.


More information about the Digitalmars-d-learn mailing list