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);
    }
}

Reply via email to