Why GNU coreutils/dd is creating a dummy file more efficiently than D's For loop?
kdevel
kdevel at vogtner.de
Thu May 23 18:20:23 UTC 2019
On Thursday, 23 May 2019 at 09:44:15 UTC, Cym13 wrote:
[...]
> Note in particular the blocksize argument. I set it to 1M but
> by default it's 512 bytes. If you use strace with the command
> above you'll see a series of write() calls, each writting 1M of
> null bytes to testfile. That's the main difference between your
> code and what dd does: it doesn't write 1 byte at a time.
His code doesn't write 1 byte at a time either. strace on my
machine reports
a blocksize of 4096. If I use this blocksize with dd it still
takes only a fraction of a second to complete.
> This results in way less system calls and system calls are very
> expensive.
His program and dd with bs=4K both have the same number of
syscalls.
> To go fast, read/write bigger chunks.
Or use rawWrite instead of write (reduces the runtime to about
1.6 s). When using write time is IMHO spent in unicode processing
and/or locking. Or write more characters at a time. The code
below takes 60 ms to complete.
y.d
```
import std.stdio, std.process;
void main()
{
writeln("Creating a dummy file");
File file = File("test.txt", "w");
ubyte [4096] nuls;
for (int i = 0; i < 50_000_000 / nuls.sizeof; ++i)
file.write(cast (char[nuls.sizeof]) nuls);
file.close();
}
```
More information about the Digitalmars-d-learn
mailing list