prinft performance problem

bearophile bearophileHUGS at lycos.com
Tue Mar 18 17:30:21 PDT 2014


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.


> I don't really see a reason why the LDC-generated code should 
> be that much slower otherwise

Nor I.


> (you can verify this by just calling __mingw_printf directly or 
> I think also by passing the -posix/-ansi flags to GCC).

OK.

If I compile this C code:

#include <stdio.h>
int main() {
     double i;
     for (i = 0; i < 200000; i++)
         printf("%f\n", i);
     return 0;
}


With (no optimizations):

gcc -ansi test2.c -o test2

The run-time is about the same (about 0.31 seconds).

But if I compile this code:

#include <stdio.h>
int main() {
     double i;
     for (i = 0; i < 200000; i++)
         __mingw_printf("%f\n", i);
     return 0;
}


The run-time is about 1.13 seconds, that is the same as the D 
version.

If I compile this version that uses __mingw_printf with:

gcc -Ofast -flto -s -ansi test2.c -o test2

The run-time is still about 1.13 seconds.

So the experiment you have suggested has given an interesting 
answer :-)


> As to why the MinGW printf() is actually slower, no idea. 
> Probably just a question of an optimized-to-hell version 
> against a simple hack to make the C99 format specifiers work.

I don't fully understand this part of your answer. And I don't 
understand how to fix the D code to make it about four times 
faster.

Can you fix ldc2 to use the same printing function as used on 
default by C code compiled by GCC? When I have to write a lot of 
floating point values this could be a significant difference in 
run-time.

Bye,
bearophile


More information about the digitalmars-d-ldc mailing list