3

So, I am new to online competitive programming and i came across a code where i am using the if else statement inside a for loop. I want to increase the speed of the loop and after doing some research i came across break and continue statements.

So my question is that does using continue really increases the speed of the loop or not.

CODE :

int even_sum = 0;
for(int i=0;i<200;i++){
      if(i%4 == 0){
         even_sum +=i;
         continue;
      }else{
           //do other stuff when sum of multiple of 4 is not calculated
       }               
  }
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

2 Answers2

3

In the specific code in the question, the code has the identical meaning with and without the continue: In either case, after execution leaves even_sum +=i;, it flows to the closing } of the for statement. Any compiler of even modest quality should treat the two options identically.

The intended purpose of continue is not to speed up code by requesting a jump the compiler is going to make anyway but to skip code that is undesired in the current loop iteration—it acts as if the remaining code had been enclosed in an else clause but may be more visually appealing and less disruptive to human perception of the code.

It is conceivable a very rudimentary compiler, or even a decent compiler but with optimization disabled, might generate a jump instruction for the continue and also a jump instruction for the “then” clause of the if statement to jump over the else clause. The latter would never be executed and would have no direct effect on program execution time, but it would increase the size of the program and thus could have indirect effects. This possibility is of negligible concern in typical modern environments, where you are unlikely to encounter such a rudimentary compiler.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

No, there's no speed advantage when using continue here. Both of your codes are identical and even without optimizations they produce the same machine code.

However, sometimes continue can make your code a lot more efficient, if you have structured your loop in a specific way, e.g.

This:

int even_sum = 0;
for (int i = 0; i < 200; i++) {
    if (i % 4 == 0) {
        even_sum += i;
        continue;
    }
    if (huge_computation_but_always_false_when_multiple_of_4(i)) {
        // do stuff
    }
}

is a lot more efficient, than:

int even_sum = 0;
for (int i = 0; i < 200; i++) {
    if (i % 4 == 0) {
        even_sum += i;
    }
    if (huge_computation_but_always_false_when_multiple_of_4(i)) {
        // do stuff
    }
}

because the former doesn't have to execute the huge_computation_but_always_false_when_multiple_of_4() function every time.

So even though both of these codes would always produce the same result (given that huge_computation_but_always_false_when_multiple_of_4() has no side effects), the first one, which uses continue, would be a lot faster.

ruohola
  • 21,987
  • 6
  • 62
  • 97