mmap file performance

Andy Valencia dont at spam.me
Thu Apr 11 16:23:44 UTC 2024


On Thursday, 11 April 2024 at 14:54:36 UTC, Steven Schveighoffer 
wrote:
> For a repeatable comparison, you should provide the code which 
> does 1MB reads.

With pleasure:

import std.stdio : writeln, File, stderr;

const uint BUFSIZE = 1024*1024;

private uint
countnl(File f)
{
     uint res = 0;
     char[BUFSIZE] buf;

     while (!f.eof) {
         auto sl = f.rawRead(buf);
         foreach (c; sl) {
             if (c == '\n') {
                 res += 1;
             }
         }
     }
     return res;
}

private uint
procfile(in string fn) {
     import std.exception : ErrnoException;
     File f;

     try {
         f = File(fn, "r");
     } catch(ErrnoException e) {
         stderr.writeln("Can't open: ", fn);
         return 0;
     }
     uint res = countnl(f);
     f.close();
     return res;
}

void
main(in string[] argv)
{
     foreach (fn; argv[1 .. $]) {
         uint res;
         res = procfile(fn);
         writeln(fn, ": ", res);
     }
}



More information about the Digitalmars-d-learn mailing list