howto count lines - fast

Jordan Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 30 13:44:26 PDT 2017


On Tuesday, 30 May 2017 at 20:37:44 UTC, Jordan Wilson wrote:
> On Tuesday, 30 May 2017 at 20:02:38 UTC, Nitram wrote:
>> After reading 
>> https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/ , i was wondering how fast one can do a simple "wc -l" in D.
>>
>> So i made a couple short implementations and found myself 
>> confronted with slow results compared to "/usr/bin/wc -l".
>>
>> How would a implementation look like in D, which is fast?
>
> Not sure if this is the fastest, but anyway
>
> void main(){
>     import std.file : read;
>     import std.conv : to;
>     import std.algorithm : count;
>
>     auto data = cast(ubyte[])read("somefile.txt");
>     auto lc = data.count('\n'.to!ubyte);
> }
>
> Jordan

I should say, if you don't care about storing the data, 
File("somefile.txt","r").byLine.count is probably more idiomatic, 
and probably just as fast.

Jordan


More information about the Digitalmars-d-learn mailing list