prinft performance problem

bearophile bearophileHUGS at lycos.com
Wed Mar 19 03:59:02 PDT 2014


David Nadlinger:

> However, as mentioned in my last message, GCC by default uses 
> the printf() function from the Microsoft C runtime, which can't 
> handle reals (i.e. C long doubles) and some of the C99 format 
> specifiers.

I missed that part of your compressed answer :-)


> If you are actually using C printf() directly in your program 
> (and not the Phobos formatting functions)

I sometimes use printf when I have to print lot of data because 
writeln is usually quite slower.


> and the Microsoft runtime covers the format specifiers you 
> need, then you can just manually write the function 
> declarations in question ("extern(C) int printf(const char*, 
> …)"). core.stdc.stdio merely contains an alias from 
> __mingw_printf() to printf().

Good, this D code runs in 0.31, about as the C version:


extern(C) nothrow int printf(const char*, ...);
int main() {
     for (double i = 0; i < 200000; i++)
         printf("%f\n", i);
     return 0;
}


This is enough for my purposes, thank you (I don't need to print 
large amounts of reals).


> If the Phobos string formatting performance is not good enough 
> for you, then the best thing to do would be to write a D 
> floating point formatting implementation and finally ditch the 
> C formatting functions.

Printing floating point values correctly and quickly is a very 
complex project :-)

Bye,
bearophile


More information about the digitalmars-d-ldc mailing list