views:

3216

answers:

9

Assume i have the following code, there are three for loop to do something. Would it run fast if i change the most outer for loop to while loop? thanks~~

int length = 200;
int test = 0;
int[] input = new int[10];

for(int i = 1; i <= length; i++) {
    for (int j = 0; j <=length - i; j++) {
        for (int k = 0; k < length - 1; k++) {
            test = test + input[j + k];
        }
    }
}
+12  A: 

No, changing the type of loop wouldn't matter.

The only thing that can make it faster would be to have less nesting of loops, and looping over less values.

The only difference between a for loop and a while loop is the syntax for defining them. There is no performance difference at all.

int i = 0;
while (i < 20){
    // do stuff
    i++;
}

Is the same as:

for (int i = 0; i < 20; i++){
    // do Stuff
}

(Actually the for-loop is a little better because the i will be out of scope after the loop while the i will stick around in the while loop case.)

A for loop is just a syntactically prettier way of looping.

jjnguy
you can add scope around the i to knock it off the stack once you are done with it :D
Polaris878
+1  A: 

No, you're still looping the exact same number of times. Wouldn't matter at all.

aquinas
A: 

It would only matter if you are using multi-thread or multiple processor programming. Then it would also depends on how you assign the loops to the various processors/threads.

Extrakun
A: 

Look at your algorithm! Do you know beforehand which values from your array are added more than one time?

If you know that you could reduce the number of loops and that would result in better performance.

Roalt
+3  A: 

you cant optimize it by changing it to while.

you can just increment speed very very very very little by changing the line

for (int k = 0; k < length - 1; k++) {

by

for (int k = 0; k < lengthMinusOne; k++) {

where lengthMinusOne is calculated before

this subtraction is just calculating almost (200x201/2) x (200-1) times and it is very little number for computer :)

ufukgun
The Java compiler will optimize that call out usually these days. But you are technically correct about that being faster.
jjnguy
Heh, I was half expecting someone to suggest changing `k++` to `++k` :D
Mike Caron
+11  A: 

This kind of micro-optimization is pointless.

  • A while-loop won’t be faster.
  • The loop structure is not your bottleneck.
  • Optimize your algorithm first.
  • Better yet, don’t optimize first. Only optimize after you have found out that you really have a bottleneck in your algorithm that is not I/O-dependant.
Bombe
A: 

Even if the hypothesis of the while loop being faster than the for loop were true (and it's not), the loops you'd had to change/optimize wouldn't be the outer ones but the inner ones, because those are executed more times.

fortran
+1  A: 

The difference between for and while is semantic :

  • In a while loop, you will loop as long as the condition is true, which can vary a lot, because you might, in your loop, modify variables using in evluating the while condition.
  • Usually, in a for loop, you loop N time. This N can be variable, but doesn't move until the end of your N loop, as usually developpers doesn't modify variables evaluated in the loop condition.

It is a way to help other to understand your code. You are not obliged not to modify for loop variables, but it is a common (and good) practice.

Clement Herreman
A: 

Completely agree with Bombe:
1) Make it work
2) Make it work right
3) Make it work fast

Alex Givant