Why is stdin.byLine.writeln so slow?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 13 15:23:50 PDT 2014


On Fri, Jun 13, 2014 at 10:02:49PM +0000, monarch_dodra via Digitalmars-d-learn wrote:
[...]
> 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).

I wrote part of that documentation, and my favorite example is matrix
formatting:

	auto mat = [[1,2,3], [4,5,6], [7,8,9]];
	writefln("[%([%(%d %)]%|\n %)]", mat);

Output:

	[[1 2 3]
	 [4 5 6]
	 [7 8 9]]

D coolness at its finest!

Whoever invented %(, %|, %) is a genius. It takes C's printf formatting
from weak sauce to whole new levels of awesome. I remember debugging
some range-based code, and being able to write stuff like:

	debug writefln("%(%(%s, %); %)", buggyNestedRange().take(10));

at strategic spots in the code is just pure win.  In C/C++, you'd have
to manually write nested loops to print out the data, which may involve
manually calling accessor methods, manually counting them, perhaps
storing intermediate output fragments in temporary buffers,
encapsulating all this jazz in a throwaway function so that you can use
it at multiple strategic points (in D you just copy-n-paste the single
line above), etc..  Pure lose.

(Speaking of which, this might be an awesome lightning talk topic at the
next DConf. ;-) Or did somebody already do it?)


T

-- 
Having a smoking section in a restaurant is like having a peeing section in a swimming pool. -- Edward Burr 


More information about the Digitalmars-d-learn mailing list