<div dir="ltr"><span style="font-size:13px">Hi!</span><div style="font-size:13px"><br></div><div style="font-size:13px">I am trying to port a program I have written earlier to D. My previous versions are in C++ and Python. I was hoping that a D version would be similar in speed to the C++ version, rather than similar to the Python version. But currently it isn't.</div><div style="font-size:13px"><br></div><div style="font-size:13px">Part of the problem may be that I haven't learned the idiomatic way to do things in D. One such thing is perhaps: how do I read large text files in an efficient manner in D?</div><div style="font-size:13px"><br></div><div style="font-size:13px">Currently I have created a little test-program that does the same job as the UNIX-command "wc -lc", i.e. counting the number of lines and characters in a file. The timings I get in different languages are:</div><div style="font-size:13px"><br></div><div style="font-size:13px">D:           15s</div><div style="font-size:13px">C++:       1.1s</div><div style="font-size:13px">Python:   3.7s</div><div style="font-size:13px">Perl:        2.9s</div><div style="font-size:13px"><br></div><div style="font-size:13px">The central loop in my D program looks like:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">        foreach (line; f.byLine) {</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">            nlines += 1;</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">            nchars += line.length + 1;</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">        }</p></div><div style="font-size:13px"><br></div><div style="font-size:13px">I have also tried another variant with this inner loop:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">        char[] line;</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">        while(f.readln(line)) {</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">            nlines += 1;</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">            nchars += line.length;</p><p style="margin:0px;font-size:14px;font-family:Menlo;background-color:rgb(255,234,213)">        }</p></div><div style="font-size:13px"><br></div><div style="font-size:13px">but in both cases this D program is much slower than any of the others in C++/Python/Perl. I don't understand what can cause this dramatic difference to C++, and a factor 4 to Python. My D programs are built with DMD 2.067.1 on MacOS Yosemite, using the flags "-O -release".</div><div style="font-size:13px"><br></div><div style="font-size:13px">Is there something I can do to make the program run faster, and still be "idiomatic D"?</div><div style="font-size:13px"><br></div><div style="font-size:13px">(I append the whole program for reference)</div><div style="font-size:13px"><br></div><div style="font-size:13px">Regards,</div><div style="font-size:13px">/Johan Holmberg</div><div style="font-size:13px"><br></div><div style="font-size:13px">=======================================</div><div style="font-size:13px"><div><div>import std.stdio;</div><div>import std.file;</div><div><br></div><div>void main(string[] argv) {</div><div>    foreach (fname; argv[1..$]) {</div><div>        auto f = File(fname);</div><div>        int nlines = 0;</div><div>        int nchars = 0;</div><div>        foreach (line; f.byLine) {</div><div>            nlines += 1;</div><div>            nchars += line.length + 1;</div><div>        }</div><div>        writeln(nlines, "\t", nchars, "\t", fname);</div><div>    }</div><div>}</div></div><div>=======================================</div><div><br></div></div></div>