Question: I'm writing some optimized C code that basically runs through an array and does something to each element. What it does depends on the current value of the element so something like:

```
for (i=0; i < a_len; i++) {
    if (a[i] == 0) {
        a[i] = f1(a[i]);
    } else if (a[i] % 2 == 0) {
        a[i] = f2(a[i]);
    } else {
        a[i] = 0;
}

```

I'm returning to C after many years working in dynamic languages, where my practice has been to try to write straightforward code and not create lots of local variables for things that I can just refer to directly, like a[i] above. *I am very much aware that best practices are to write readable code and trust that the compiler is smarter than you and will do good optimizations.*

If I were writing the code above in assembler, I would load a[i] into a register once and then just use that value each time because I know that a[] is private memory and won't change between references. However, even a smart compiler might do a load every time because it can't be sure that the memory hasn't changed. (Or do I have to explicitly declare "a" volatile for the compiler to not make this optimization?).

So, my question is: should I expect better performance by rewriting with a local variable like so:

```
for (i=0; i < a_len; i++) {
    val = a[i];
    if (val == 0) {
        a[i] = f1(val);
    } else if (val % 2 == 0) {
        a[i] = f2(val);
    } else {
        a[i] = 0;
}

```

Or does stuff like -O3 take care of this automatically for me? The code I'm optimizing takes days to run, so even modest improvements will make a difference.

Answer: 
You say "If I were writing the code above in assembler..." so I assume you know assembly language.

My advice: Have a look at the compiler output on critical sections of code, see what's really going on.
Optimization hint
-----------------

I would probable first look at having `var` as a pointer instead i local vaiable to probable be better. then you dont use double storing of the variable as well

```
int* var;//Int or whatever type a[] is
for (i=0; i < a_len; i++) {
    val = &a[i];
    if (*val == 0) {
        f1(val);//// Set the valur inside f1
    } else if (*val % 2 == 0) {
        f2(val);// Set the valur inside f2
    } else {
        *val = 0;
}

```

A hint for optimizing your code could be to avoid using '%' operator when you are not interested in the result. It depends on your compiler but this has proven way faster for me (using a macro for readability):

```
#define is_divisible(dividend, divisor) ((((dividend)/(divisor)) * (divisor))==(dividend))

```

use:

```
else if (is_divisible(val,2)) {

```

This is faster, at least on most cases I have tested.

Edit: true that the profit is not so large when using modula calculations using only '% 2' But if you ever are looning at a larger calue than 2 to do modula operation and are just interested in of the modula returns zero then my macro is faster in all the compilers i have used