6

I was told that a while loop was more efficient than a for loop. (c/c++) This seemed reasonable but I wanted to find a way to prove or disprove it.

I have tried three tests using analogous snippets of code. Each containing Nothing but a for or while loop with the same output:

  1. Compile time - roughly the same
  2. Run time - Same
  3. Compiled to intel assembly code and compared - Same number of lines and virtually the same code

Should I have tried anything else, or can anyone confirm one way or the other?

mreff555
  • 1,049
  • 1
  • 11
  • 21
  • 10
    They are the same. The only difference is logical sequence of operations and corresponding readability. – i486 Jan 10 '16 at 21:40
  • 15
    Whoever told you that - don't listen to that person anymore. – Violet Giraffe Jan 10 '16 at 21:42
  • Possible dup https://stackoverflow.com/questions/3629174/which-loop-is-faster-while-or-for – oleksii Jan 10 '16 at 21:48
  • Ha ha, Violet Giraffe. Great advice. Already following it. I never want to see that horrible person again. That's what I get for taking programming advice from a Statistical Thermodynamics Professor. – mreff555 Jan 10 '16 at 21:58
  • "I was told that a while loop was more efficient than a for loop." - Complete and utter nonsense. – Christian Hackl Jan 10 '16 at 22:24
  • 2
    Kudos for making an effort to verify what you were told and not simply taking it at face value. – Disillusioned Jan 10 '16 at 22:48

3 Answers3

4

All loops follow the same template:

{
// Initialize
LOOP: 
    if(!(/* Condition */) ) {
        goto END
    }

    // Loop body

    // Loop increment/decrement
    goto LOOP
}
END:

Therefor the two loops are the same:

// A
for(int i=0; i<10; i++) {
    // Do stuff
}

// B
int i=0;
while(i < 10) {
    // Do stuff
    i++;
}

// Or even
int i=0;
while(true) {
    if(!(i < 10) ) {
        break;
    }

    // Do stuff
    i++;
}

Both are converted to something similar to:

{
int i=0;
LOOP: 
    if(!(i < 10) ) {
        goto END
    }

    // Do stuff

    i++;
    goto LOOP
}
END:

Unused/unreachable code will be removed from the final executable/library.

Do-while loops skip the first conditional check and are left as an exercise for the reader. :)

Caramiriel
  • 7,029
  • 3
  • 30
  • 50
1

Certainly LLVM will convert ALL types of loops to a consistent form (to the extent possible, of course). So as long as you have the same functionality, it doesn't really matter if you use for, while, do-while or goto to form the loop, if it's got the same initialization, exit condition, and update statement and body, it will produce the exact same machine code.

This is not terribly hard to do in a compiler if it's done early enough during the optimisation (so the compiler still understands what is actually being written). The purpose of such "make all loops equal" is that you then only need one way to optimise loops, rather than having one for while-loops, one for for-loops, one for do-while loops and one for "any other loops".

It's not guaranteed for ALL compilers, but I know that gcc/g++ will also generate nearly identical code whatever loop construct you use, and from what I've seen Microsoft also does the same.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

C and C++ compilers actually convert high level C or C++ codes to assembly codes and in assembly we don't have while or for loops. We can only check a condition and jump to another location.

So, performance of for or while loop heavily depends on how strong the compiler is to optimize the codes.

This is good paper on code optimizations:

http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf.

frogatto
  • 28,539
  • 11
  • 83
  • 129