Question: Can somebody explain what is going on here with this small program?

```
#include<stdio.h>

int main()
{
    float a=0.577;
    float b=0.921; 
    float c; 
    int i;

    for (i=0;i<100000000;i+=1){
        c=0.7*a-0.2*b;
        //a=0.145*c+2.7*b;
    }

    printf ("%.3f\n",c);
}

```

Note, there is a line commented out.

I compiled it first without the line and then with the line. (Used `gcc -O2 ...`). And measured the processing time. I was very surprised to find out that execution time was `0.001s` versus `2.444s`. And this doesn't make much sense. Or rather, there must be some logic behind this.

Can you please explain what is going on and how to mitigate this problem?

I work on an program that process huge amount of data and it seems to me that I run into very same performance problem there.

I was considering switching from floats to integers but it seems that with integers it behaves the same.

EDIT: At the end the solution was trivial and logical. So I thank for all answers and explanations!

Answer: 
In the first instance the calculated value was constant. The compiler would have calculated `c = 0.7 * 0.577 - 0.2 * 0.921` at **compile time**. Its even free to optimize out the loop as nothing changes within it (`a`, `b` & `c` are invariant).

In the second instance, `a` and `c` vary for each iteration so have to be computed `100000000` times.
Without the commented out line, the compiler can optimize out the whole loop. The value being set doesn't change with respect to the loop.

With the commented out line, the value of `a` changes at each start of the loop, so the loop can't be optimized out.

That is, your program and this one:

```
int main()
{
    float a=0.577;
    float b=0.921; 
    float c; 
    int i;

    c=0.7*a-0.2*b;
    for (i=0;i<100000000;i+=1){
        //a=0.145*c+2.7*b;
    }

    printf ("%.3f\n",c);
}

```

produce the same answer if and only if that line is commented.