Why is stdin.byLine.writeln so slow?

Jyxent via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 13 14:23:33 PDT 2014


On Friday, 13 June 2014 at 21:08:08 UTC, monarch_dodra wrote:
> On Friday, 13 June 2014 at 20:48:16 UTC, Jyxent wrote:
>> I've been playing around with D and noticed that:
>>
>> stdin.byLine.writeln
>>
>> takes ~20 times as long as:
>>
>> foreach(line; stdin.byLine) writeln(line);
>>
>> I asked on IRC and this was suggested:
>>
>> stdin.byLine(KeepTerminator.yes).copy(stdout.lockingTextWriter)
>>
>> which is slightly faster than the foreach case.
>>
>> It was suggested that there is something slow about writeln 
>> taking the input range, but I'm not sure I see why.  If I 
>> follow the code correctly, formatRange in std.format will 
>> eventually be called and iterate over the range.
>
> Because:
> stdin.byLine.writeln
> and
> foreach(line; stdin.byLine) writeln(line);
> Don't produce the same output. One prints a range that contains 
> strings, whereas the second repeatedly prints strings.
>
> Given this input:
> line 1
> line	2
> Yo!
>
> Then "stdin.byLine.writeln" will produce this string:
> ["line 1", "line\t2", "Yo!"]
>
> So that's the extra overhead which is slowing you down, because 
> *each* character needs to be individually parsed, and 
> potentially escaped (eg: "\t").
>
> The "copy" option is the same as the foreach one, since each 
> string is individually passed to the writeln, which doesn't 
> parse your string. The "lockingTextWriter" is just sugar to 
> squeeze out extra speed.

Hah.  You're right.  I had seen writeln being used this way and 
just assumed that it printed every line, without looking at the 
output too closely.

Thanks for clearing that up.


More information about the Digitalmars-d-learn mailing list