# Loop Unswitching – Is that Necessary

Let’s take a look at this code

Sample code 1:

```  int i, w, x, y;
for (i = 0; i < 1000; i++) {
x[i] = x[i] + y[i];
if (w)
y[i] = 0;
}
```

This code adds two arrays and do something else depending on the variable w.

A problem with this code is that we have to check the value of w for 1000 times .

To solve this problem, we can improve the Sample code 1 as follows,

Sample code 2:

```  int i, w, x, y;
if (w) {
for (i = 0; i < 1000; i++) {
x[i] = x[i] + y[i];
y[i] = 0;
}
} else {
for (i = 0; i < 1000; i++) {
x[i] = x[i] + y[i];
}
}

```

With the Sample code 2, we only have to check the variable w once. The sample codes are excerpted from Wikipedia and the technique we are using here is called "loop unswitching".

For the C (or C++ code) like above, when compiled with gcc with version greater than 3.4 and " -funswitch-loops" option, ( or -O3 if you are too lazy), the compiler - gcc - will automatically does the loop unswitching for you. That means, you don't have to worry about that your code is not optimized.

The Sample code 1 is shorter and therefore easier to ready on your source code control systems. The Sample code 2 can be optimized by the compiler. The point here is, let's write it in an easy way and let the compiler does the rest for you. The code in Sample 1 can be blamed for not being optimized and therefore it can be recommended for refactoring.

I don't have a concrete ideas how PHP, Java and Scala (that uses some kind of JVMs) handle this kind of optimization.

It looks like that Oracle's (formerly Sun) JVM does it by default since Java 6 therefore we don't have to optimize the code in Sample 1 because the JVM does the optimization for us.

My guess, is that, with Scala we can leave the code as in Sample 1 for the sake of simplicity and we don't have to sacrifice any performance.

If you have any comments or some nice source code to prove or disapprove this, please leave a comment!

Scroll Up