# system gcc optimization flags on 7.1-RELEASE



## ducu_00 (Feb 4, 2009)

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:

```
#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
> 
> ...



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
> 
> ...



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
> 
> ...



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.


----------



## Djn (Feb 4, 2009)

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.


----------



## ducu_00 (Feb 5, 2009)

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.


----------



## Djn (Feb 5, 2009)

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.)


----------

