print function
Artur Skawina via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 5 04:35:14 PST 2016
On 02/05/16 08:04, cy via Digitalmars-d-learn wrote:
> On Thursday, 4 February 2016 at 15:32:48 UTC, Artur Skawina wrote:
>> void print(A...)(A a) {
>> foreach (N, ref e; a)
>> write(e, N==A.length-1?"\n":" ");
>> }
>
>>> will be unrolled at compile time
>
> Mind if I elaborate on this a bit? If that is unrolled, I understand it will unroll into several calls to write, as in print("1","2","3") => write("1"," ");write("2"," ");write("3","\n");
Yes, and they are all using the same `write!(string, string)` instance
(there will be one for every printed type, `write!(typeof(A[N]), string)`).
> Any literal string you pass to std.stdio.write will be expanded into 1 fwrite invocation per character.
D's std lib implementations are sometimes really awful, but in
this case it's not actually that bad:
print("hi","there");
->
fwrite("hi", 1, 2, 0x7ff68d0cb640) = 2
fwrite(" ", 1, 1, 0x7ff68d0cb640) = 1
fwrite("there", 1, 5, 0x7ff68d0cb640) = 5
fwrite("\n", 1, 1, 0x7ff68d0cb640) = 1
What happens between `print` and `fwrite` - I have no idea.
Years ago I had to investigate why phobos showed up in the
perf profile of a program, when the only used part was some
`write` call used to print diagnostics. What I saw made me
never use or look at D's std lib again. Except for meta
programing and toy/example programs where it doesn't matter.
artur
More information about the Digitalmars-d-learn
mailing list