system gcc optimization flags on 7.1-RELEASE

Hello everybody.
I am a relatively new freebsd user but an old linux one. And a totally new freebsd forum member ;)
I am not a passionate developer but I like challenges coming from the developers. Here's one.

Given the simple code:
HTML:
#include <stdio.h>
#include <math.h>

#define NMAX 5000000
int main(){

        int i, j, prime, rez = 0;

        for (i = 2; i <= NMAX; i++) {
                prime = 1;
                for (j = 2; j <= sqrt(i); j++) {
                        if (i%j == 0) {
                                prime=0;
                                break;
                        }
                }
                if (prime == 1) {
                        rez++;
                }
        }

        printf("%d\n", rez);
        return 0;
}

Lets build and run it on a freebsd 7.1 (RELEASE) system (gcc version 4.2.1 20070719):
[root@fbsd71:~/tmp/ducu]# gcc -o prim prim.c -lm
[root@fbsd71:~/tmp/ducu]# time ./prim
348513

real 0m23.150s
user 0m23.063s
sys 0m0.001s

The next step is to optimize the machine code:

[root@fbsd71:~/tmp/ducu]# gcc -O1 -o prim prim.c -lm
[root@fbsd71:~/tmp/ducu]# time ./prim
348513

real 0m23.097s
user 0m23.051s
sys 0m0.001s
[root@fbsd71:~/tmp/ducu]# gcc -O1 -ftree-vrp -o prim prim.c -lm
[root@fbsd71:~/tmp/ducu]# time ./prim
348513

real 0m24.312s
user 0m24.058s
sys 0m0.027s

It seems like there's nothing to optimize here. Hmmm...

After installing gcc43 from packages:
[root@fbsd71:~/tmp/ducu]# gcc43 -o prim prim.c -lm
[root@fbsd71:~/tmp/ducu]# time ./prim
348513

real 0m23.285s
user 0m23.259s
sys 0m0.001s
[root@fbsd71:~/tmp/ducu]# gcc43 -O1 -o prim prim.c -lm
[root@fbsd71:~/tmp/ducu]# time ./prim
348513

real 0m22.989s
user 0m22.963s
sys 0m0.001s
[root@fbsd71:~/tmp/ducu]# gcc43 -O1 -ftree-vrp -o prim prim.c -lm
[root@fbsd71:~/tmp/ducu]# time ./prim
348513

real 0m6.996s
user 0m6.937s
sys 0m0.010s
[root@fbsd71:~/tmp/ducu]#

There is something to optimize, though.

It all started from some performance comparisons of some code compiled on a linux platform versus the same code compiled on freebsd. After a few days, I tracked the problem down to this optimization flag (tree-vrp).

Is this by design (to ignore the flag)?

Thank you.
 
Unless I'm misreading you, it seems to slow things down with gcc42?
Since that's the base system compiler for now, it makes sense that it's not in the default CFLAGS.

Oh, and it seems like we might not see gcc43 in base anytime soon - see this.
 
The ideea is that '-ftree-vrp' produces no (visible) optimization as of gcc42 but a _huge_ (that code is 4 times faster) performance gain with gcc43. It seems they changed the propagation engine between 4.2 and 4.3. And, by the way, the flag is activated by '-O2', the default for CFLAGS, if I understood correctly.
After more reading (thank you for the link), I (finally) understood that the base system compiler is there because it produces good os code and makes the system self-contained, not because it is the best compiler for the user. That's why we have all the gcc versions on the ports/packages system.
 
Indeed. And they also try to keep the same gcc version through a major version (e.g. FreeBSD 6), since it makes things easier.

(For one thing, it means you get the same behavior and bugs.)
 
Back
Top