Why is stdin.byLine.writeln so slow?

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 13 14:08:07 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list