Juneyong Park
Juneyong Park

Reputation: 31

Conditional inside for loop or lots of for loop?

I'm trying to make my code work differently for different values of i in for loop, but I don't know if I should make the conditional go inside the loop or just create multiple for loops for enhanced speed.

My English does seem quite inefficient at explaining things, so here's an example:

for (int i = 1; i < 31; i++) {
    if (i < 11) {
        System.out.println(3*i);
    } else if (i<21) {
        System.out.println(2*i);
    } else System.out.println(i);
}

or

for (int i = 1; i < 11; i++) System.out.println(3*i);
for (int i = 11; i < 21; i++) System.out.println(2*i);
for (int i = 21; i < 31; i++) System.out.println(i);

It would really help if the reason why one of them might be better or not could be explained as well. Thank you in advance :>

Upvotes: 3

Views: 90

Answers (5)

Joop Eggen
Joop Eggen

Reputation: 109567

The 3 for loops are faster (not important here), as there no longer is an if-else-if at every i-step. More important the three loops are far more readable because the if cascade is removed.

However using j = i + 1 the first loop can be converted to:

final int n = 30;
for (int j = 0; j < n; j++) {
    int k = n/10 - j/10;
    System.out.println(k * (j + 1));
}

Because of the division this will probably not be faster. However the removal of the if-cascade is an improvement. The expressions are harder to interprete while reading, but they specify a bit of calculating logic, which the mere stating of if conditions would not: one could change 30 with 300 and still everything would make sense.

Or

for (int j = 0; j < 3; ++j) {
    for (int i = 1 + j*10; i < 11 + j*10; i++) {
        System.out.println((3-j)*i);
    }
}

Upvotes: 0

xingbin
xingbin

Reputation: 28279

Make the code readable is more important. The performance difference is very small, which can be ignored in most cases. Here is the experement result on my computer:

pattern 1:

  • run 100000 times cost 7548 milli seconds
  • run 1000000 times cost 70180 milli seconds

pattern 2:

  • run 100000 times cost 7536 milli seconds
  • run 1000000 times cost 70535 milli seconds

Upvotes: 1

Persixty
Persixty

Reputation: 8589

Unless driven by performance considerations let readability lead you. The second one is surely easier to understand. Though I'd recommend using block statements:

for (int i = 1; i < 11; i++) {
    System.out.println(3*i);
}
for (int i = 11; i < 21; i++) {
    System.out.println(2*i);
}
for (int i = 21; i < 31; i++) {
    System.out.println(i);
}

Of course you could make a formula:

for (int i = 1; i < 31; i++) {
    int fac=3-((i-1)/10);
    System.out.println(fac*i);
}

Though that seems fairly unreadable too it might be the best approach if the equivalent were many for-loops or a number of loops you couldn't determine at compile time.

Upvotes: 0

Hasnain Ali Bohra
Hasnain Ali Bohra

Reputation: 2180

The first single loop analysis :- Number of variable initialized 1. Number of comparisons :-

  1. 1 < 31

  2. 1 < 11

  3. 2 < 31

  4. 2 < 11 so on.

Hence for 1 to 10 number of comparison 20.

for 11 to 20 number of comparison 30. for 21 to 30 number of comparison 30.

so total 80 comparison for single loop. but

for (int i = 1; i < 11; i++) System.out.println(3*i);
for (int i = 11; i < 21; i++) System.out.println(2*i);
for (int i = 21; i < 31; i++) System.out.println(i);

total comparison 31.

So the seperate loop is good instead of if else ledder.

Upvotes: 2

Eran
Eran

Reputation: 393836

Enhanced speed should not be a consideration. The differences (if any) would be negligible.

You should choose the more readable version. When using a for loop, you usually mean you wish to perform the same action N times. In your case you want to perform 3 different actions, each a different number of times (or for different values of i). Therefore it makes more sense to have 3 loops.

for (int i = 1; i < 11; i++) {
    System.out.println(3*i);
}
for (int i = 11; i < 21; i++) {
    System.out.println(2*i);
}
for (int i = 21; i < 31; i++) {
    System.out.println(i);
}

Upvotes: 3

Related Questions