This D2 program runs in about 5.13 seconds on my PC, on Windows: import std.stdio; void main() { auto f = File("bytes_test.dat", "wb"); ubyte[3] RGB; foreach (_; 0 .. 1_000_000) f.rawWrite(RGB); } While this C program runs in about 0.14 seconds: #include <stdio.h> int main() { FILE *f = fopen("bytes_test.dat", "wb"); unsigned char RGB[3] = {0}; int i; for (i = 0; i < 1000000; i++) fwrite(RGB, 1, 3, f); return 0; } See also: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30858 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30866
The reason for this is because rawWrite calls flush, not once, but twice every time you call it, on the underlying file. This is absolutely absurd, and is a massive performance bottleneck, especially when it's not needed because the file may very well have already been opened for writing in binary mode to begin with. In this particular example, it's flushing to disk every 3 bytes.
Just a note: I don't know, if this has also been an issue using linux, but nowadays it works on linux.
Just was doing some development and found this same bug. This is absolutely unnecessary, especially if the file is already open in binary mode. We currently don't store what mode the file is in, but we absolutely could (we are allocating a heap struct for this, no reason we can't store that). And only switch if necessary. This should be an easy change and I'm kind of surprised this has been open for so long.
@canopyofstars created dlang/phobos pull request #7590 "Fix issue 7033: File.rawWrite is slow on Windows" fixing this issue: - Fix issue 7033 https://github.com/dlang/phobos/pull/7590
dlang/phobos pull request #7590 "Fix issue 7033: File.rawWrite is slow on Windows" was merged into master: - 8643ecad370508f6adf7ba2a34eee1924e47a2f7 by starcanopy: Fix issue 7033 https://github.com/dlang/phobos/pull/7590