prinft performance problem

David Nadlinger code at
Wed Mar 19 01:22:28 PDT 2014

On 19 Mar 2014, at 1:30, bearophile wrote:
> David Nadlinger:
>> LDC/Win32 uses the MinGW output/formatting functions, as e.g. the 
>> printf() from the MSCRT can't handle reals.
> I am compiling the C code with the same gcc that ldc2 is using on 
> default on Windows, as explained in the ldc2 installation procedure.
> […]
> Can you fix ldc2 to use the same printing function as used on default 
> by C code compiled by GCC?

Doing this would be easy, in fact that's how it was before we started 
serious work on LDC/MinGW.

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.

Thus, using the MSCRT functions will cause serious problem for many D 
programs (and the DMD/Phobos test suites), as all the floating point 
formatting and printing functions in Phobos depend on the C runtime 
functions like snprintf().

long doubles and C99 format strings are not as widespread in C code, 
thus you explicitly have to define __MINGW_USE_ANSI_STDIO to get 
printf() and friends mapped to the MinGW functions in C code (I would 
have thought that some command line switches also affect that, but 
apparently I misremembered).

If you are actually using C printf() directly in your program (and not 
the Phobos formatting functions) 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().

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.


More information about the digitalmars-d-ldc mailing list