Why is stdin.byLine.writeln so slow?

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 13 15:02:49 PDT 2014


On Friday, 13 June 2014 at 21:17:27 UTC, Ali Çehreli wrote:
> On 06/13/2014 02:08 PM, monarch_dodra wrote:
>
> > Given this input:
> > line 1
> > line    2
> > Yo!
> >
> > Then "stdin.byLine.writeln" will produce this string:
> > ["line 1", "line\t2", "Yo!"]
>
> Do you mean writeln() first generates an array and then prints 
> that array?

No, it just receives a range, so it does range formating. eg:
"[" ~ Element ~ ", " ~ Element ... "]".

> I've always imagined that it used the range interface and did 
> similar to what copy() does.

That wouldn't make sense. Then, if I did "[1, 2, 3].writeln();", 
it would print:
123
instead of
[1, 2, 3]

> Is there a good reason why the imagined-by-me-range-overload of 
> writeln() behaves that way?
>
> Ali

As I said, it's a range, so it prints a range. That's all there 
is to it.

That said, you can use one of D's most powerful formating 
abilities: Range formating:
writefln("%-(%s\n%)", stdin.byLine());

And BOOM. Does what you want. I freaking love range formatting.
More info here:
http://dlang.org/phobos/std_format.html#.formattedWrite

TLDR:
%( => range start
%) => range end
%-( => range start without element escape (for strings mostly).


More information about the Digitalmars-d-learn mailing list